0

Can someone tell me whats wrong with that code?And i cant use malloc because i havent learn it in class.I mean can i make a 2d array of strings without malloc and if yes how i am i supposed to write an element when i want to change it/print it/scan it.Thanks in advance

int main() {

    size_t x,y;
    char *a[50][7];

    for(x=0;x<=SIZEX;x++)
    {
        printf("\nPlease enter the name of the new user\n");
        scanf(" %s",a[x][0]);

        printf("Please enter the surname of the new user\n");
        scanf(" %s",a[x][1]);

        printf("Please enter the Identity Number of the new user\n");
        scanf(" %s",a[x][2]);

        printf("Please enter the year of birth of the new user\n");
        scanf(" %s",a[x][3]);

        printf("Please enter the username of the new user\n");
        scanf(" %s",a[x][4]);

    }

    return 0;
}
  • "*`char *a[50][7];`*" is a 2D-array of *pointers* to `char`. – alk Jan 04 '16 at 18:31
  • "*can i make a 2d array of strings*" of which size? How many elements in which dimension? How long should the strings be? – alk Jan 04 '16 at 18:32
  • i mean i want to make a 2D array of pointers that point to strings. Do i need to say how long the strings can be ?If yes lets say [50] –  Jan 04 '16 at 18:45
  • Just declare some memory on the stack and make your 50*7 pointer point to it. You also want to make up some logic to determine which size had been allocated to any of those 50*7 pointers. – alk Jan 04 '16 at 18:59

2 Answers2

2

So, you need a 2d array of strings (char arrays). One way to achieve this would be to allocate a 3d array of char as:

char x[50][7][MAX_LENGTH];

You can think as having a matrix of array start (of pointers) and then, another dimension to give depth to your matrix (i.e. storage space for your string).

Your approach is also fine, as long as you are willing to allocate manually using malloc or similar storage space for your strings.

Paul92
  • 8,827
  • 1
  • 23
  • 37
  • no 3d array .I need to make a 2D array of pointers that they point to strings. –  Jan 04 '16 at 18:46
  • It is a 2d array of strings, which, in 3, is equivalent to a 3d array of chars – Paul92 Jan 04 '16 at 18:47
  • Oh i get it.But i cant do it with my approach and without malloc? And if i have to use malloc how the code would be. I ask because i havent learnt malloc in class :/ –  Jan 04 '16 at 18:49
  • No. You basically declare pointers there, and you need to make them point to some allocated memory. You can either allocate that memory dinamically with malloc, or allocate memory for each pointer in-place, as I do – Paul92 Jan 04 '16 at 18:51
  • Sorry but because i am really new to c can you show me an example where you allocate memory for each pointer in-place while having a 2D array.Actually like allocate memory for all the a[x][1] and then a[x][2]... –  Jan 04 '16 at 18:55
  • Imagine you want a single string. You can say either char *p and allocate memory afterwards, or say char p[50] and treat p as you have allocated memory for it (indeed, you actually allocated space for 50 characters and you use them to store the string). Do the same for a 2d matrix and you get a 3d data structure, and you can use a 2d matrix of pointers to access the strings. – Paul92 Jan 04 '16 at 18:58
0

can i make a 2d array of strings without malloc

Sure. Let's reduce this to 2*3:

#include <stdio.h>

char * pa[2][3] = {
   {"a", "bb","ccc"},
   {"dddd", "eeeee", "ffffff"}
  };

int main(void)
{
  for (size_t i = 0; i < 2; ++i)
  {
    for (size_t j = 0; j < 3; ++j)
    {
      printf("i=%zu, j=%zu: string='%s'\n", i, j, pa[i][j]);
    }
  }
}

Output:

i=0, j=0: string='a'
i=0 j=1: string='bb'
i=0, j=2: string='ccc'
i=1, j=0: string='dddd'
i=1, j=1: string='eeeee'
i=1, j=2: string='ffffff'
alk
  • 69,737
  • 10
  • 105
  • 255