1

I have some problems about char *.

  1. How many char we can store in a variable of char *?
  2. Is it safe to use char * (instead of fixed amount char array) as a string in C?
7
  • 1. exactly sizeof(char*). 2. In terms of safety char* and char[] are the same
    – fukanchik
    Commented Oct 7, 2022 at 11:45
  • 1
    char * doesn't store any char elements. It stores address of char element. It may be standalone variable or element of array, char * doesn't care.
    – dimich
    Commented Oct 7, 2022 at 11:52
  • 2
    There's no magic, no "policeman", to protect the coder. It's up to the coder to make sure that buffers don't overflow whether accessed via a pointer ( char *p ) or via an array index ( arr[ n ] ). It's up to the coder, not the language.
    – Fe2O3
    Commented Oct 7, 2022 at 11:54
  • 4
    That does not assign 7 characters but only 1 address of a string literal. The string literal itself provides the required memory. Your char* only knows where it is.
    – Gerhardh
    Commented Oct 7, 2022 at 12:43
  • 3
    Assigning a new value (address) to a char* does not modify the string. It will make the variable point to some different string (if there is a string at the new location). Whether it is a proper way to modify a string via the pointer (`variable[1] = '1';) depends on the memory where the pointer points to.
    – Gerhardh
    Commented Oct 7, 2022 at 13:32

4 Answers 4

3

How many char we can store in a variable of char *?

The type char * is a pointer type. Usually sizeof( char * ) is equal to 4 or 8 bytes depending on the used system.

Is it safe to use char * (instead of fixed amount char array) as a string in C?

You may initialize a character array with a string literal as for example

char s[] = "hellp";

then you may change the stored string in the array like for example

s[0] = 'H';

because copies of characters of the string literal are stored in the array.

If you will initialize a pointer by a string literal

char *s = "hello";

then you may not write

s[0] = 'H';

because any attempt to change a string literal results in undefined behavior.

3

How many char we can store in a variable of char *?

None. A char * variable stores the address of a single char object. That object may be the first character in a string:

char *ps = "This is a string";  // ps stores the *address* of 'T' character

    +---+       +---+
ps: |   |  ---> |'T'|
    +---+       +---+
                |'h'|
                +---+
                |'i'|
                +---+
                 ...
                +---+
                |'g'|
                +---+
                | 0 |
                +---+

or it may be the first character in a sequence that isn't a string (not 0 terminated, or a 0 byte appears in the middle of the sequence):

char seq[] = { 'a', 'b', 'c', 'd' }; // seq is not a string
char *pq = seq;  // pq stores the address of the first element of seq

    +---+           +---+
pq: |   | ---> seq: |'a'| seq[0]
    +---+           +---+
                    |'b'| seq[1]
                    +---+
                    |'c'| seq[2]
                    +---+
                    |'d'| seq[3]
                    +---+

or it may store the address of a single character that isn't part of a larger sequence:

char c = 'A';
char *pc = &c; // pc stores the address of the variable c

    +---+         +---+
pc: |   | ---> c: |'A'|
    +---+         +---+

Is it safe to use char * (instead of fixed amount char array) as a string in C?

Strings (including string literals) are always stored in arrays of character type. A char * only stores an address - you must allocate an array of character type to store the string itself. You can do that by declaring an array (either at block or file scope):

char foo[20];
...
strcpy( foo, "This is a string" );

Under most circumstances, an expression of type "N-element array of T" will be converted, or "decay", to an expression of type "pointer to T" and the value of the expression will be the address of the first element in the array.

In the statement

strcpy( foo, "This is a string" );

the expression foo has type "20-element array of char"; however, in this context, that expression "decays" to type "pointer to char" and its value is equivalent to &foo[0].

Thus, most of the time when we're working with strings, we're working with expressions of type char *. But a char * object is not a string. It doesn't store any char values.

Alternately, you can allocate memory dynamically using either malloc or calloc:

char *foo = calloc( 20, sizeof *foo );
strcpy( foo, "This is a string" );

The variable foo stores the address of the first element of a block of 20 char objects that were allocated by calloc. The advantage of using dynamic memory is you can extend or shrink that buffer as necessary - an array cannot be resized once it has been defined.

1
  1. How many char we can store in a variable of char *?

A variable of type char* holds exactly 1 address of some memory. Without assigning some valid address, you cannot store any characters using that pointer variable. This "valid address" can be the address of a string literal, some array or some dynamically allocated memory. It solely depends on the size of that memory how many characters can be stored. Or in case of a string literal, if the memory can be written at all or if it is read only.

It is up to you to remember the size of that buffer.

  1. Is it safe to use char * (instead of fixed amount char array) as a string in C?

They are very different. A pointer only points to some address. It does not bring any memory for the characters. An array also reserves the memory for the characters. It depends on what you need to do with that.

1

For the second question:

That depends. Let's say you have allocated memory with

c=(char *) malloc(10*sizeof(char));
    if(c==NULL){
        printf("Failed to allocate enough memory");
        exit(1);
    }

But you got the input from the user with gets or scanf(" %c",c); Then the user may enter a string longer than ten characters. But if you take it with scanf(" %10[^\n]s",c); Then the user can't enter longer than 10 characters. Which makes your code safer.

3
  • You are right. Thanks for the correction. English is not my native tongue. Commented Oct 7, 2022 at 12:02
  • Better, but there's still no room if there are 10 characters PLUS a '\0'... Buffer overrun and Undefined Behaviour... Better to malloc( (10 + 1) * sizeof *c ) (notice the compiler knows that c should be a pointer to a char, so *c means the size of the right size!!
    – Fe2O3
    Commented Oct 7, 2022 at 12:05
  • 1
    IN FACT, I just noticed... the scanf format specifier is incorrect... You want " %10[^\n]" Leave off the s... That is a serious mistake in the format specifier...
    – Fe2O3
    Commented Oct 7, 2022 at 12:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.