# String array iteration causing segfault

**URL:** <https://community.unix.com/t/string-array-iteration-causing-segfault/333583>\
**Category:** Programming\
**Created:** [August 27, 2013, 3:04pm UTC](https://community.unix.com/t/string-array-iteration-causing-segfault/333583 "2013-08-27T15:04:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rupeshkp728](https://community.unix.com/letter_avatar/rupeshkp728/32/6_449c533f586939b6e5f246632a46421f.png) [@rupeshkp728](https://community.unix.com/u/rupeshkp728)\
**Post date:** [August 27, 2013, 3:04pm UTC](https://community.unix.com/t/string-array-iteration-causing-segfault/333583/1 "2013-08-27T15:04:03Z")

</div>

I am populating an array of string and print it.  
But it going in infinite loop and causing segfault.

```nohighlight
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

```nohighlight
j = 0 Name = yahoo len = 5
j = 1 Name = rediff len = 6
j = 2 Name = facebook len = 8
j = 3 Name = len = 0
j = 4 Name = len = 0
j = 5 Name = len = 0
j = 6 Name = len = 0
j = 7 Name = len = 0
j = 8 Name = len = 0
j = 9 Name = len = 0
.
.
.
12976 Segmentation fault ./test_code

```

What is wrong with the code?

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [August 27, 2013, 4:32pm UTC](https://community.unix.com/t/string-array-iteration-causing-segfault/333583/2 "2013-08-27T16:32:03Z")

</div>

You made it an array of arrays, guaranteeing it will never be NULL. array[j] just calculates array + (j\*50), which isn't going to be zero.

If you want to store a null pointer, you'll have to store pointers. Then you get `address stored at (base_address + j)` which is quite capable of being NULL.

```nohighlight
char *strings[]={
                "yahoo",
                "rediff",
                "facebook",
                NULL };

```

---

<div class="post-metadata">

**Author:** ![rupeshkp728](https://community.unix.com/letter_avatar/rupeshkp728/32/6_449c533f586939b6e5f246632a46421f.png) [@rupeshkp728](https://community.unix.com/u/rupeshkp728)\
**Post date:** [August 27, 2013, 4:48pm UTC](https://community.unix.com/t/string-array-iteration-causing-segfault/333583/3 "2013-08-27T16:48:23Z")

</div>

Thanks coronna for the reply.  
Using while(Name[j][0] != NULL) solved my problem.

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [August 27, 2013, 4:50pm UTC](https://community.unix.com/t/string-array-iteration-causing-segfault/333583/4 "2013-08-27T16:50:13Z")

</div>

That means you cannot store an empty string in your array though -- it will be the same as NULL, i.e. first character holding a zero.

---

<div class="post-metadata">

**Author:** ![rupeshkp728](https://community.unix.com/letter_avatar/rupeshkp728/32/6_449c533f586939b6e5f246632a46421f.png) [@rupeshkp728](https://community.unix.com/u/rupeshkp728)\
**Post date:** [August 27, 2013, 4:53pm UTC](https://community.unix.com/t/string-array-iteration-causing-segfault/333583/5 "2013-08-27T16:53:09Z")

</div>

yes it seems so and so inspite of being zero loop went infinetely.

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [August 27, 2013, 4:59pm UTC](https://community.unix.com/t/string-array-iteration-causing-segfault/333583/6 "2013-08-27T16:59:59Z")

</div>

I don't understand your question or statement.

---

<div class="post-metadata">

**Author:** ![rupeshkp728](https://community.unix.com/letter_avatar/rupeshkp728/32/6_449c533f586939b6e5f246632a46421f.png) [@rupeshkp728](https://community.unix.com/u/rupeshkp728)\
**Post date:** [August 27, 2013, 5:04pm UTC](https://community.unix.com/t/string-array-iteration-causing-segfault/333583/7 "2013-08-27T17:04:37Z")

</div>

I meant whatever you said in your second comment was correct and so inspite of being zero loop went infinetely.

---

<div class="post-metadata">

**Author:** ![anon57720281](https://community.unix.com/letter_avatar/anon57720281/32/6_449c533f586939b6e5f246632a46421f.png) [@anon57720281](https://community.unix.com/u/anon57720281)\
**Post date:** [September 4, 2013, 5:52am UTC](https://community.unix.com/t/string-array-iteration-causing-segfault/333583/8 "2013-09-04T05:52:43Z")

</div>

alternatively like so:

```nohighlight
#include <stdio.h>
#include <string.h>

const char * names[] = {
                "yahoo",
                "rediff",
                "facebook",
                NULL
                };

int main(int argc, char* argv[])
{
    const char ** p;
    for( p = names; *p ;p++)
    {
            printf(" Name = %s len = %d\n", *p, strlen(*p));
    }

    return 0;

}

```
