Click here to Skip to main content
14,429,343 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am populating an array of string and print it.
But it going in infinite loop and causing segfault.

char Name[][50] = {
                "yahoo",
                "rediff",
                "facebook",
                NULL
                };

main(int argc, char* argv[])
{
    int j = 0;

    while(Name[j] != NULL)
    {
            printf("j = %d    Name = %s    len = %d\n", j, Name[j], strlen(Name[j]));
            j++;
    }

    return(0);

}


output is
j = 0 volumeName = yahoo len = 5
j = 1 volumeName = rediff len = 6
j = 2 volumeName = facebook len = 8
j = 3 volumeName = len = 0
j = 4 volumeName = len = 0
j = 5 volumeName = len = 0
j = 6 volumeName = len = 0
j = 7 volumeName = len = 0
j = 8 volumeName = len = 0
j = 9 volumeName = len = 0
.
.
.
12976 Segmentation fault ./test_code

What is wrong with the code?
Posted
Comments
pasztorpisti 27-Aug-13 15:57pm
   
Your code would work as it is if you declared the array as char* Name[] instead of char Name[][50]. With char Name[][50] your NULL item simply fills up the last char array[50] with zeros but the pointer to this last array will never be NULL. With he char* Name[] declaration the last item of the array will indeed be a NULL pointer as you wanted and not an array of 50 chars that is automatically casted by C into a non-NULL char pointer that points to an array of 50 zero chars.
rupeshkp728 27-Aug-13 16:00pm
   
Thanks pasztorpisti for the explaination.
I had kept it to char* Name[] earlier.
But I have to do some strcpy later on in the code which was failing due to unallocated memory and so I moved to two dimensional array.
pasztorpisti 27-Aug-13 16:27pm
   
You are welcome! I understand, in that case using a fixed sized char array is the good solution. You was fooled by C because in this language an array expression is automatically converted into a pointer that points to the first item of the array. Be careful with this! BTW, you can avoid using a NULL terminator with the following code:
size_t array_lenght = sizeof(Name)/sizeof(Name[0])
for (size_t i=0; i<array_length; ++i)
{
...
}

This trick works with both declarations if you don't want a terminator item at the end of the array.
rupeshkp728 27-Aug-13 16:45pm
   
Thanks pasztorpisti.
As per the code design I have to keep the null terminated array only.
Rate this:
Please Sign up or sign in to vote.

Solution 3

Salut! I propose this

char Name[][50] = {
                   {"yahoo"},
                   {"rediff"},
                   {"facebook"},
                   {NULL}
                  };

main()
{
  int j = 0;
 
  while( *Name[j] != NULL)
   {
     printf("j = %d; Name = %s; len = %d\n", j, Name[j], strlen(Name[j]));
     j++;
    }
}
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

Change
while(Name[j] != NULL)

line to

while(Name[j][0] != NULL)



then your code will work perfectly
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Thanks MuthukaruppanP.
It resolved the issue.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100