vimp
There are some differences. The s[] is an array, but *s is a pointer.
For an example, if two declarations are like char s[20], and char *s respectively,
then by using sizeof() we will get 20, and 4.
The first one will be 20 as it is showing that there are 20 bytes of data.
But second one is showing only 4 as this is the size of one pointer variable.
For the array, the total string is stored in the stack section,
but for the pointer, the pointer variable is stored into stack section,
and content is stored at code section. And the most important difference is that,
we cannot edit the pointer type string.
So this is read-only. But we can edit array representation of the string.
char* is how you declare a pointer to a char variable. It’s useful when you want
a string with unknown length.
1st example:
char name[10];
strcpy (name, “type_your_name_here”); //overwrites the first argument with the second.
Here you’re reserving 10 pieces of memory. You might use them all or your name
might just be “Jack” which, if we account for the ‘\0’ special character that
comes at the end of every string, takes only 5 memory chunks. That means you
have 5 remaining pieces that you’re not using.
Maybe your name is longer that 10 characters, where will you store the extra ones
then? You won’t be able to. Because you gave a static declaration to your array
of chars.
2nd example:
char *name;
This means that you just declared the pointer variable where you’ll store the
address of the first character in your string. It gives more freedom and
flexibility to your usage. Whether your name is long or short, the predefined
string functions like “strcpy” and “strcat” can handle memory allocation for you.
--------------------------------------------------------------------
--------------------------------------------------------------------
1) char *
--------------------------------------------------------------------
Let’s start with two programs.
/ * Program1.c */
int main()
{
char *p1 = "Hello"; //here we can change whole string at once
//but not single letters it becomes diff
p1[0] = 'A'; // Error: Segmentation fault
return 0;
}
and
/* Program2.c */
int main()
{
char chr = 'Z';
char *p1 = "Hello";
p1 = &chr; // OK: Value of p1 can be changed
return 0; //Z output
}
So, if you compile these two programs, both of them compile without any error.
But when you execute Program1.c it will give Segmentation Fault (on Linux) or
Access violation (on Windows).
Well, it’s predictable. You see, two things involved here:
char *p1
and
"Hello World"
p1 is just a char pointer, we have not written anything that makes it a constant.
This guy will just hold the address of a memory location. And Data Type of that
location shall be a char. However, "Hello" is a string literal, its a constant
and p1 holds the starting address of this char array or string literal.
Diagrammatically,
h 3197
e 3198
.....
o 3202//memeory allocated for storing variable value
p1 3012//memeory allocated to p1
So p1 is innocent guy just holding a memory address, however, but the memory
location starting from 3197 is read-only as it holds a string literal or
string constant. So if you try to update that via p1 it will give segmentation
fault or access violation. You are not allowed to modify that. You can't
modify any location from 3197 to 3202.
2) char []
--------------------------------------------------------------------
Before proceeding further lets have a look what the classical text on
C Programming has to say about this:
Assuming variables:
int a[10];
and
int *pa;
There is one difference between an array name and a pointer that must be kept
in mind. A pointer is a variable, so pa=a and pa++ are legal. But an array name
is not a variable; constructions like a=pa and a++ areillegal.
- The C Programming Language by Brain W. Kernighan & Dennis M. Ritchie
So, technically, when you write int a[10] you declare a kind of constant pointer
holding the address of first location of 10 consecutive memory locations.
Now the story is completely reversed. In this case
/* Program1b.c */
int main()
{
char p1[] = "Hello"; //here we can change single chars
//but not whole string it becomes diff
p1[0] = 'A'; // Valid
return 0;
}
Valid, because the location where string literal is not a CONSTANT (though bounded by length).
/* Program2.c */
int main()
{
char chr = 'Z';
char p1[] = "Hello";
p1 = &chr; // Error: p1 is not a valid L-Value
return 0;
}
To embed this program on your website, copy the following code and paste it into your website's HTML: