Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   array of string (http://www.go4expert.com/forums/array-of-string-t22607/)

reta 2Jul2010 02:41

array of string
 
hi to all

I write the code below to search string in array for 6 times, if I input a string not exist in array more than one time first time it return not found and this true but then it print found although that's string not exist in array.

the error is here
arr[ii]=str;

I need to put the content of str in arr[ii] not to make arr[ii] point to location of str. how I can do that??


Code:

#include <stdio.h>
    #include<string.h>
    #include<stdbool.h>
   
    int main(int argc, char *argv[])
    {
        int i,index,ii;
        char str[512];
        char *j,*temp;
      bool found;
        char *arr[12]={"hello","world","hi to all","sad","this","code","not","work"\
    ,"help","me","please ","."};
        int arr2[12]={1,1,1,1,1,1,1,1,1,1,1,1};
   
   
        for(ii=0;ii<6;ii++)
        {
            printf("######################\n");
    printf("loop:%d\n",ii);
        found=0;
        scanf("%s",str);
       
        for(i=0;i<12;i++)
        {
                temp=arr[i];
                if((strcmp(str,temp))==0)
                {
                    arr2[i]=(arr2[i]+1);
                found=1;
           
            index=i;
                }
        }
            if (found==0)
          {
                arr[ii]=str;
   
          }
       
        for(i=0;i<12;i++)
        {
                printf("%s-- ",arr[i]);
                printf("%d ",arr2[i]);
                printf("\n");
        }
            if (found==0)
            printf("not found\n");
    else printf("found at %d\n",index); 
        printf("######################\n");
        }
    }


xpi0t0s 2Jul2010 15:07

Re: array of string
 
arr doesn't store strings. It's of type char*, so it stores pointers only. If you want to store char data, you need a char array, not a pointer to char.

Here's the problem. Suppose str represents 512 bytes at memory location 1000. arr[1]=str; sets arr[1] to 1000. If str contains "hello", arr[1] DOES NOT contain "hello", it's just a pointer. It contains 1000 and *currently* points to "hello". So now if you overwrite str so it now holds "goodbye" and do arr[2]=str;, arr[2] and arr[1] will now both point at 1000 and when you dereference each they will both find "goodbye". arr[1] never contained "hello", it only ever contained 1000, so it will point to whatever is at 1000, i.e. whatever is currently in str.

You always need to be careful with pointers in C. They're really easy, but you have to be aware what they're pointing at, and it's confusion over that which makes pointers appear difficult.

So the solution is to make arr a 2D array of strings, which you can do either with char *arr[12] and a bunch of malloc statements (remember "free" so that you don't leak memory), or you can do char arr[12][512]. arr[1]=str then won't work; you'll have to do strcpy(arr[1],str).


All times are GMT +5.5. The time now is 01:57.