Why this works:
#include <stdio.h>
void slice(char *st, int m, int n)
{
int i = 0;
while ((i m) < n)
{
st[i] = st[i m];
i ;
}
st[i-1] = '\0';
}
int main()
{
char st[] = "Hello";
slice(st, 1, 6);
printf("The value of string is %s\n", st);
return 0;
}
And this doesn't:
#include <stdio.h>
void slice(char *st, int m, int n)
{
int i = 0;
while ((i m) < n)
{
st[i] = st[i m];
i ;
}
st[i-1] = '\0';
}
int main()
{
char*st = "Hello";
slice(st, 1, 6);
printf("The value of string is %s\n", st);
return 0;
}
In first I initialized my string using:- char st[]="Hello";(using array)
and in latter I used:- char*st="Hello";(using pointer)
I'm kind of getting confused between these 2 initialization types, what's the key difference between declaring a string by using char st[]="Hello";
and by using char*st = "Hello";
CodePudding user response:
If you have read-only strings then you can use const char* st = "hello";
or simply char* st = "hello";
. So the string is most probably be stored in a read-only memory location and you'll not be able to modify it.
However, if you want to be able to modify it, use the malloc function:
char *st= (char*) malloc(n*sizeof(char)); /* n-The initial size that you need */
// ...
free(st);
**So to allocate memory for st
, count the characters ("hello"-strlen(st)=5) and add 1 for this terminating null character , and functions like scanf and strcpy will add the null character;
so the code becomes :
#include <stdio.h>
void slice(char *st, int m, int n)
{
int i = 0;
while ((i m) < n)
{
st[i] = st[i m];
i ;
}
st[i-1] = '\0';
}
int main()
{
char *st =malloc(6*sizeof(char)) ;
const char *cpy="hello";
strcpy(st, cpy); /* copies the string pointed by cpy (including the null character) to the st. */
slice(st, 1, 6);
printf("The value of string is %s\n", st);
return 0;
}
you can fill your string also by a for loop or by scanf() .
in the case of a large allocation you must end your code with
free(st);
CodePudding user response:
This is an artifact of old syntax in C:
char * s = "Hello world!";
is a non-const character pointer to const memory. It is still permitted by syntax, but the string is still not a mutable object. To be pedantic it should really be written as:
const char * s = "Hello world!";
In contrast:
char s[] = "Hello world!";
allocates a local (on the stack), mutable array and copies the string data to it (from wherever the non-mutable copy is stored in memory). Your function can then do as it likes to your local copy of the string.
CodePudding user response:
With char st[] = "Hello";
, st[]
is a modifiable array of characters. The call slice(st, 1, 6);
takes the array st
and converts to a pointer to the first element of the array. slice()
then receives that pointer, a pointer to modifiable characters.
With char *st = "Hello";
, st
is a pointer that points to a string literal "Hello"
. With the call slice(st, 1, 6);
, the function recives a copy of the pointer - a pointer to the string literal. Inside slice()
, code st[i] = ...
is attempting to modify a string literal, that is undefined behavior (UB). It might work, it might fail, it might work today and fail tomorrow - it is not defined.
Do not attempt to modify a string literal.
... passing strings to a function ...
In both cases, code does not pass a string to slice()
, but a pointer to a string. Knowing that subtle distinction helps in understanding what is truly happening.