-2

I am trying to print a character string from a 2D array using a randomNum. Here is what I have:

int randomNum = 0;
char names[2][] = { {"Joe\0"},{"John\0"} };
printf("%s ",names[randomNum][]);

This does not work however.

J. Doe
  • 1
  • It would be helpful to say what you expect it to do... And what it does instead. – mlibby Jun 23 '18 at 16:46
  • The most obvious problem is that you have an empty [] in your print statement. – mlibby Jun 23 '18 at 16:47
  • Yes, remove it. – purec Jun 23 '18 at 16:48
  • You don't need `\0` at the end of your strings. The compiler puts the null terminator at the end of a string constant for you already. – lurker Jun 23 '18 at 16:52
  • 1
    Hi and welcome to Stack Overflow. You should take a look at [ask] for some tips on how to ask good questions. In the meantime, please explain what you mean by "does not work" so that we don't have to guess. – Lasse V. Karlsen Jun 23 '18 at 16:55
  • https://stackoverflow.com/questions/10003270/gcc-array-type-has-incomplete-element-type#10003410 – purec Jun 23 '18 at 17:05
  • You say "This does not work" but these are compiler errors, not runtime errors. You can only leave the compiler to set the array dimension if it is the outermost one. Here, you should give, for example, `char names[][42] = { "Joe", "John" };`. Also the output has a syntax error, it should be `printf("%s", names[randomNum]);` – Weather Vane Jun 23 '18 at 17:10
  • Or if you are fine with `names` being *read-only*, then simply declare and initialize an *array of pointers* with the *string literals* `"Joe"` and `"John"`, e.g. `char *names[] = { "Joe", "John" };` – David C. Rankin Jun 24 '18 at 06:33

1 Answers1

0

Here is an example code of what I believe should be implemented:

#include <stdio.h>
#include <stdlib.h>

#define NUMBER_OF_STRINGS  2

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

    /* The capacity of the array is to hold up to 2 strings*/
    char *names[NUMBER_OF_STRINGS] = { "Joe",
                                        "John"};
                /*
                *
                *  names[0][0] = 'J' -> first element of names[0]  -  J
                *  names[0][1] = 'o' -> second element of names[0] -  o
                *  names[0][2] = 'e' -> third element of names[0]  -  e
                *
                *  names[1][0] = 'J' -> first element of names[0]  -  J
                *  names[1][1] = 'o' -> second element of names[0] -  o
                *  names[1][2] = 'h' -> third element of names[0]  -  h 
                *  names[1][3] = 'n' -> third element of names[0]  -  n 
                *
                */

    /*Here we pass the pointer of the array of strings and we index the first string*/                                          
    printf("%s \n", names[randomNum]);

    /*Here is how to access each of the elements separetly*/
    printf("Here we print the name Joe character by character\n");
    printf("%c\n", names[0][0]);
    printf("%c\n", names[0][1]);
    printf("%c\n", names[0][2]);

    printf("Here we print the name John character by character\n");
    printf("%c\n", names[1][0]);
    printf("%c\n", names[1][1]);
    printf("%c\n", names[1][2]);
    printf("%c\n", names[1][3]);

    return (0);
}

To store a single string you need a char* name (character pointer). To store multiple strings it is required to use a char** names. Which is equivalent to char* names [NUMBER_OF_STRINGS]. The double pointer means that we create a pointer to a space in the memory which has a pointer of the same type. In other words in this case it is the pointer to the array of strings.

Iliya Iliev
  • 151
  • 4