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;
}
4
  • "char *a[50][7];" is a 2D-array of pointers to char. Commented Jan 4, 2016 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? Commented Jan 4, 2016 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] Commented Jan 4, 2016 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. Commented Jan 4, 2016 at 18:59

2 Answers 2

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.

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

6 Comments

no 3d array .I need to make a 2D array of pointers that they point to strings.
It is a 2d array of strings, which, in 3, is equivalent to a 3d array of chars
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 :/
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
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]...
|
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'

Comments

Your Answer

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