0

How to declare array of strings in C. Is it like

char str[100][100] ={"this","that","those"};

If so how to access the values .. can i travers like this? (It does not give any compilation error ..but shows some additional garbage characters)

int i ,j;
char  c[100][100] = {"this","that"};
for(i = 0 ;c[i] != '\0';++i)
 for(j = 0; c[i][j] != '\0';++j)
   printf("%c",c[i][j]);

Is it necessary to add '\0' at end of eac string..for ex:

 char c[100][100]={"this\0","that\0"}
3
  • 1
    printf("%s",c[i]) would print the string at index i. Commented Apr 8, 2012 at 7:06
  • That's a nice idea....thanks....will %s prints whitespace? Commented Apr 8, 2012 at 7:11
  • 1
    yes,prints until the eof string is reached i.e '\0' Commented Apr 8, 2012 at 7:23

3 Answers 3

1

How to declare array of strings in C

It is Ok, but you will have to be extremely careful of buffer-overflow when dealing with these strings

can i travers like this?

Note that the condition in the first for loop: for(i = 0 ;c[i] != '\0';++i) is probably wrong, and will fail since c[i] is an array, whose address is not 0. You should probably iterate the outer array by numbers [until you read all elements], and not until you find some specific character. You can do that by maintaining a different variable n, which will indicate how many elements does the array currently have.

Is it necessary to add '\0' at end of eac string..for ex:

No - the compiler add it to you, it is just fine without adding the '\0' to the string.

Sign up to request clarification or add additional context in comments.

4 Comments

so i<100 is condition there?=>If i have only two elements and the rest space is filled later,this condition will do more useless iterations ? How to avoid that?
It will fail if C has fewer than 100 strings, too.
i have changed the comment...added that comment just before you edited your comment...anyway what is the answer for my question in that comment?
@JinuJD: You could maintain a different variable n, which will indicate how many strings are currently in the array, and iterate while i < n. Otherwise you might fail if there are no empty strings, or if an empty string, which is usually a feasible value will appear in the middle of the array.
1
  1. Yes, you can declare an array of strings that way.

  2. No, you can't traverse it like that, the condition on your outer loop is bad - a string (char *) will never be equal to a character '\0'. The inner loop is fine.

  3. No, you don't need to add the '\0', that will happen automatically.

2 Comments

so what should be the condition in outer loop?
@Jinu, you can check cloudy goose's answer for a working example. Just watch out for the case where your array is full, since that answer won't work in that case.
1

c[i] is a pointer, so it has nothing to do with '\0'
so instead you should check c[i][0]
The compiler will add '\0' for you when you input a string like "this"

char str[100][100] ={"this","that","those"};
int main()
{
    int i ,j;
    char  c[100][100] = {"this","that"};
    for(i = 0 ;c[i][0] != '\0';++i)
    {
        for(j = 0; c[i][j] != '\0';++j)
            printf("%c",c[i][j]);
    }
}

2 Comments

so is this condition sufficient in outer loop c[i]!=NULL (or just c[i])?
@JinuJDNo, c[i] is an address, and is never NULL. Also, if c[i][0]: is enough depends on the data. If you are guaranteed to have less then 100 strings, and you are guaranteed that the empty string is not a feasible value in the array, and you are guaranteed the 100th entree in the array is marked with being the [only] empty string, it will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.