0

How would someone represent strings in C, knowing that in an array of char, I kind of need to know ahead of time how many characters the array may hold, and if I were to change the string in the same variable, it will cause a problem. So would a pointer to a char always be better ?

Omar Rida
  • 23
  • 5
  • please edit the question and provide a [mcve] example – OldProgrammer Sep 01 '22 at 16:27
  • If the array of char has an initializer, you can omit the length and let the compiler set it automatically. For example `char test[] = "12345";` is the same as `char test[6] = "12345";` because the string literal initializer has 6 `char`s including the null terminator. This applies to arrays in general, omitting the length from the definition will set the length to one plus the maximum index of the initialized elements. – Ian Abbott Sep 01 '22 at 16:29
  • 1
    *"So would a pointer to a char always be better?"* No, for the reason you stated - you might want to change the text of a string literal pointed to. You choose the appropriate method. If you want to chnage the length of an array you can use `malloc` and `realloc`. – Weather Vane Sep 01 '22 at 16:30
  • 2
    Both are used. You should be equally comfortable using either approach — and you should always know which way you're using. See [this answer](https://stackoverflow.com/questions/48734000#48734567). – Steve Summit Sep 01 '22 at 16:30
  • 2
    Strings are always null terminated arrays of `char` in C; even string literals are represented as arrays of `char`. Note that attempted modification of a string literal causes undefined behavior. Arrays decay to pointers in most expressions, so you end up working with pointers a lot. – ad absurdum Sep 01 '22 at 16:30
  • 1
    It turns out you can't *not* use pointers to refer to strings. Even though C strings are always arrays, in C all array references automatically "decay" into pointer references, so you always end up accessing strings using pointers (whether you realize it or not). That's why functions like `strlen` and `strcpy` accept parameters of type `char *`. But for any string you're working with, it's important to remember whether it is or might be (a) a string literal, (b) an array you've declared, or (c) some `malloc`'ed memory you've allocated. Those things determine what actions are possible. – Steve Summit Sep 01 '22 at 16:45

1 Answers1

3

Here are the main differences :

  1. If you use a pointer to a char, you have two choices :
char *s; // You need to allocate memory to this later, at runtime. 
char *s = "Hello World"; // This is a read-only string. You cannot change this later.
  1. If you use a array of char, you need to define a strict length.
char c[10]; // Define a buffer of max 10 chars. If last char is \0, it's a c string.

So, If you want a read-only string, use the read-only pointer to char.

If you want a strict length, use a char array.

If you want to allocate memory at runtime, use a pointer to char.

0xRyN
  • 852
  • 2
  • 13