I have some problems about char *
.
- How many
char
we can store in a variable ofchar *
? - Is it safe to use
char *
(instead of fixed amountchar
array) as a string in C?
CodePudding user response:
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.
CodePudding user response:
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(" [^\n]s",c);
Then the user can't enter longer than 10 characters. Which makes your code safer.
CodePudding user response:
How many
char
we can store in a variable ofchar *
?
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.
CodePudding user response:
- 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.
- 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.