0

I am learning 2d arrays in C and im trying to get the user's name in C and store it in a 2d array. I found out that C don't have String data type and can only accepts string with data type char.

Now I am successful in getting the name from the user but i can't store the name into the separate 2d array. Here the code i tried:

#include <stdio.h>
#include <cstring>
int main()
{   
    int to=0,y=1,z=0,end=0;
    char *ticketS[100][3];
    char name[50];
    printf("\nENTER PASSENGER'S NAME: ");
    gets(name);
    printf("%s",name);
    strcpy(ticketS[z][0], name);
    printf("%s", ticketS[z][0]);
    return 0;
}

My input is: test name

My expected output is:

test name
test name

The actual output is:

test name

I can't seem to find any examples regarding my problem. Any help would be greatly appreciated.

user13539846
  • 425
  • 2
  • 5
  • 13
  • 1
    `ticketS[z][0]` is an uninitialized pointer that does not point to any valid storage that would allow you to copy anything to that location -- yet. – David C. Rankin Jun 12 '20 at 09:56
  • 2
    Yes. And why are they pointers at all? Looks like you instead wanted 3 arrays each of 100 characters. So drop the asterisk. – underscore_d Jun 12 '20 at 09:57
  • 2
    Even then the current indexing is backwards -- instead declaring 100 arrays of 3-pointers each. Even if you drop the additional level of indirection and declare `char ticketS[100][3];` you have storage for 100 2-character strings (recall to be a *string* the array (or allocated block) must include the *nul-terminating* character `'\0'` as the next character after the last character in your string... so you require one additional character of storage for each stored string) – David C. Rankin Jun 12 '20 at 10:01
  • 1
    See [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) – David C. Rankin Jun 12 '20 at 10:04
  • 1
    That just copies the pointer, not the string. So it's only valid in the same scope as the array `name` it refers to. That doesn't sound like it's what you want. – underscore_d Jun 12 '20 at 10:19

1 Answers1

1

Assuming you wanted to create 2 arrays with 100 chars each, and copy the name given by the user to the 2D array:

#include <stdio.h>
#include <string.h>
int main()
{   
    int to=0,y=1,z=0,end=0;
    char ticketS[2][100];
    char name[50];
    printf("\nENTER PASSENGER'S NAME: ");
    gets(name);
    printf("%s\n",name);

    strcpy(ticketS[0], name); // a for-loop would work as well
    strcpy(ticketS[1], name);

    printf("%s\n%s\n", ticketS[0], ticketS[1]);
    return 0;
}

A few things you should pay attention to:

  1. The indexing is wrong. char *ticketS[100][3]; creates 100 arrays with 3 chars each.
  2. Using char *ticketS[100][3]; you just declare the variable, however, you don't initialize it
  3. Null-terminator '\0' here plays a role. strcpy copies until it finds a Null-terminator. Therefore, to get your wanted result you would need to copy the string a few times, or use a different copying method (not strcpy). This is also why I used %s twice with printf.
  4. In the code exmaple I gave, ticketS[0] is a pointer to the 1st 100 char array.

Hope you find this useful.

Y.R.
  • 371
  • 1
  • 7
  • Great! This works. Seems like i misunderstood 2d arrays in C. Thanks! – user13539846 Jun 12 '20 at 10:23
  • "char *ticketS[100][3]; creates 100 arrays with 3 chars each" No, it creates an array of 100 items, each item an array of 3 pointer to `char*`. – Lundin Jun 12 '20 at 12:37