array of string

Discussion in 'C' started by reta, Jul 1, 2010.

  1. reta

    reta New Member

    Joined:
    Jun 15, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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");
            }
        }
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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).
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice