-2

I want to create an array of N positions. In every position, I want to store a 10 character string. This is what I tried. In the example given, N = 15. But this number can change.

char *userArray[10] = malloc(sizeof(char[10])*15);

When compiling iget an error: invalid initializer.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128

3 Answers3

0

You want pointer to the array of 10 char elements, not pointer to char

char (*userArray)[10] = malloc(15 * sizeof(*userRrray));

Then you can use it as normal array:

Examples:

strcpy(userArray[3], "Hello");

print("%s\n", userArray[3]);
print("%c\n", userArray[3][4]);

To return this array you need to define function like this:

char (*allocate(size_t size))[10]
{
    char (*userArray)[10] = malloc(size * sizeof(*userArray));

    return userArray;
}
0___________
  • 60,014
  • 4
  • 34
  • 74
  • If I want to return this array, what should type should I write in the function header? "char* [10] funcion(){}"?? –  Jan 29 '22 at 16:45
0

char *userArray[10] = malloc(sizeof(char[10])*15);

You have not cleared whether your variable userArray is stack or heap allocated and what's the dimension of the userArray?

Arrays in C can be of two types (based on where they are stored):

  1. Stack Memory Arrays
  2. Heap Memory Arrays

Stack Memory Arrays

You can initiate a array which is going to be stored on stack memory like this:
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

Now, to obtain array's length use this:

size_t length_arr = sizeof(arr) / sizeof(*arr);

Heap Memory Arrays

You can initiate a array which is going to be stored on heap memory like this:
int *arr = (int *)calloc(10, sizeof(int));

Now, do not use sizeof to obtain arr's length because sizeof(arr) will return the size of the pointer, which in my case is 8. So, you need to keep track of the length of array.

Also, do not forget to free() the heap allocated resource after use. For freeing heap allocated resources snippet will be:

free(arr);

NOTE: Must include stdlib.h header file before using functions like malloc(), calloc(), realloc() or free().

Darth-CodeX
  • 2,166
  • 1
  • 6
  • 23
0

char *userArray[10] = malloc(sizeof(char[10])*15);

malloc(3) returns a pointer, and *userArray[10] is not a pointer. It's an array of 10 pointers. You can't initialize an array — any array of anything — with a pointer. An array can be initialized only with values. The compiler, not you, decides the array's address.

The definition you want is

char userArray[15][10]

That's an array of 15 elements, with each element itself an array of 10 chars. Another way to say the same thing:

typedef char elem_t[10];  
elem_t  userArray2[15];

A typedef like that is useful when the array elements represent something that occurs elsewhere in the program, so all such variables have the same type and size.

Now I will offer some controversial advice: don't use malloc(3) with constant arguments. If you know the value at compile time, you don't need malloc. malloc is for dynamic memory, memory whose size is determined at runtime, by a value not known at compile time.

Someone will warn you that large arrays can't be allocated on the stack (technically, automatic storage in C). That's true, but you have static storage available, either local to a function or at file scope.

There are a few rare cases that justify using statically sized dynamic memory. While you're learning the language, you can assume they don't exist. In 35 years of using C, I doubt I've seen a dozen examples.

James K. Lowden
  • 7,574
  • 1
  • 16
  • 31