2

I'd like to have an array of fixed length strings in c in the form of a pointer so I can dynamically allocate the memory for it. I can see plenty of reference to arrays of pointers to string, but not to what I want to achieve.

What I want is to be able to declare a pointer to char[MAX_STRING_LENGTH] so I can then dynamically allocate a contiguous block of memory for all the strings:

char *(names[MAX_STRING_LENGTH]);  // This won't work
names = (some cast)malloc(NUM_STRINGS * MAX_STRING_LENGTH);

And then access the array of strings:

strcpy(name, names[stringIndex]);

How do I declare the variable and cast the pointer from malloc?

Dan
  • 179
  • 1
  • 11
  • So if i follow you, you want to have a big char array so you can do your own allocation inside of it? – litelite Sep 01 '17 at 13:53
  • I want to do a single allocation of a two-dimensional array where the second dimension is fixed – Dan Sep 01 '17 at 13:55
  • "length" of a _string_ usually does not include is _null character_. Consider `strlen()`. Code may need `MAX_STRING_LENGTH` --> `MAX_STRING_LENGTH + 1` to use `names` correctly. – chux - Reinstate Monica Sep 01 '17 at 14:28

4 Answers4

4

You declare names as an array of pointers, not a pointer to arrays. That would be

char (*names)[MAX_STRING_LENGTH];  // Names is a pointer to an array of char

Now you can allocate memory for the pointer:

// Allocate memory for NUM_STRINGS arrays and assign the pointer to names
names = malloc(NUM_STRINGS * sizeof *names);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks, I tried that but stumbled upon the cast from malloc. How would you do such cast? – Dan Sep 01 '17 at 14:03
  • 3
    @Dan [In C you don't need to cast the return from `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) (and probably shouldn't). – Some programmer dude Sep 01 '17 at 14:03
  • Can one omit parentheses around *names in declaration? – Paul Dydyshko Sep 29 '22 at 10:34
  • @PaulDydyshko No. `char (*names)[MAX_STRING_LENGTH];` defines a pointer to an array of `MAX_STRING_LENGTH` characters. But `char *names[MAX_STRING_LENGTH];` defines an array of `MAX_STRING_LENGTH` *pointers* to characters. The first is a pointer to an array, the second is an array of pointers. Very big difference. :) – Some programmer dude Sep 29 '22 at 10:42
2

You want a pointer to array MAX_LENGTH of char Using cdecl (note that cdecl doesn't understand things like MAX_LENGTH so we use a number here instead):

% cdecl
Type `help' or `?' for help
cdecl> declare names as pointer to array 256 of char;
char (*names)[256]

we get that the proper declaration is

char (*names)[MAX_LENGTH];

However this still isn't very useful, because reallocation would be very costly because it possibly could have to move each and every string to a new location. So I suggest that you'd still use a pointer to pointer to char.

1

Someprogrammerdude already provided the solution.

Generally, since C/C++ types can become somewhat tricky, you can utilize any half-decent IDE to look up correct type definitions.

This trick relies on the C++11 auto keyword, which determines a variable type from its initializer. The resulting type can still be used in other C/C++ versions and is not restricted to C++11.

In your case, specify a type that you know and that can be transformed into the type you want:

char names[10][10];

Then assign an auto variable with the desired type:

auto names2 = &names[0];

Inspect the actual type of names2 and use it. For example, in Visual Studio, just hover the mouse over names2 to see the type. Most IDEs will display the type by hovering over either the auto keyword or the defined variable.

For the example, hovering would reveal the type char (*names2)[10].

grek40
  • 13,113
  • 1
  • 24
  • 50
  • 1
    `auto names2 = &names[0];` --> _warning: type defaults to 'int' in declaration of 'names2'_ and will certainly not work with this C post. Perhaps you are coding in another language? – chux - Reinstate Monica Sep 01 '17 at 14:31
  • 1
    @chux oh damn it, I guess I fell for the VS C is not really C trap again... it's C++11 `auto` and I'll have to update my answer. The result of this type-check can still be applied to C/C++ though – grek40 Sep 01 '17 at 14:35
0

You need to understand the concept of Pointer Arrays.Refer the book "The C programming Language" By Dennis M. Ritchie & Kernighan. Specially the pointer and array part.There are some amazing stuffs to read. Probably you will get a more deeper concept of pointers and arrays after reading and solving some problems given in the book.

krpra
  • 466
  • 1
  • 4
  • 19