This question already has answers here:
What is array to pointer decay?
(11 answers)
Closed 12 months ago.
If arrays decay into a pointer shouldn't char string[] = "Hello" also point to the first element like char* string = "Hello" or char *(string) = "Hello".
The first one dcays too
#include <stdio.h>
int main() {
char string[] = "Hello";
printf("First char = %c", *string);
}
gives
First char = H
Related
This question already has answers here:
Returning an array using C
(8 answers)
Closed 1 year ago.
How do I return a char array with any length in C?
char string[](int, int);
doesn't work; is there a way to return a char array?
You are unable to do that in C.
Instead, return a pointer.
char *string(int length, int unused) {
char *s = calloc(x, sizeof(char));
return s;
}
This question already has answers here:
What is the difference between char s[] and char *s?
(14 answers)
Closed 1 year ago.
Good day,
I have a function ft_strupcase which takes in a char*, upper-cases it, and returns the parameter. The issue arose during the testing, namely using the function in a main. The following program results in a segmentation fault:
int main()
{
char *hey = "hEy";
printf("%s\n", ft_strupcase(hey));
}
whereas this variation doesn't:
int main()
{
char hey[] = "hEy";
printf("%s\n", ft_strupcase(hey));
}
Isn't *str and str[] the same? Doesn't str[i] = *(str + i)? Why do I encounter a segfault then?
int main()
{
char *hey = "hEy";
printf("%s\n", ft_strupcase(hey));
}
In this code, hey points to a string literal, which is a constant. Then ft_strupcase modifies the thing you pass it a pointer to. So this code attempts to modify a constant.
You can't modify a constant. That's what it means for something to be constant.
int main()
{
char hey[] = "hEy";
printf("%s\n", ft_strupcase(hey));
}
Here, hey is an array of characters, initialized from a constant. The array is modifiable since the array entries are not constants.
If you have int i = 3;, you can modify i, but you can't modify the 3. The first code tries to modify the thing on the right side of the = by passing a function that modifies the thing pointed to a pointer to it. The second code modifies the thing on the left side of the =, which is perfectly legal.
This question already has answers here:
Pointer to Integer Array versus Double Pointer to Integer
(3 answers)
Is an array name a pointer?
(8 answers)
Why should I always enable compiler warnings?
(20 answers)
Closed 3 years ago.
The following code will segfault.
char* input = "12.34"; //< this is only to simplify the example.
char buffer[30] = { 0 };
memcpy(buffer, input, strlen(input));
char* part1 = strsep(&buffer, ".");
The following code will not.
char* input = "12.34"; //< this is only to simplify the example.
char buffer[30] = { 0 };
memcpy(buffer, input, strlen(input));
char* ptr = buffer; //< Only diff.
char* part1 = strsep(&ptr , ".");
When passed by reference (&) as a function argument, why does the difference between char** and char*[30] matter?
If you look at the man for strsep, it needs the double pointer because it tries to assign the pointer.
"char *strsep(char **stringp, const char *delim);
...
and *stringp is updated to point past the token"
Basically if you have an array, you can't just tell the head of the array to be in a new place. You can however make a pointer to any element in that array and then change the value of the pointer (thereby making it point to a different element if you so wish).
This question already has answers here:
What is the difference between char s[] and char *s?
(14 answers)
Closed 10 years ago.
Is there a difference between:
char string = "name";
const char* point = string;
vs
const char string[] = "name";
Will you please explain the difference too?
Yes.
The first simply points to a read only section of memory, the declaration really should be:
const char* string = "name";
The second creates an array long enough to hold the string "name" (so, four characters plus one for the null terminator) and copies the string inside the allocated space.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert integer to string in C?
I would like to calculate the length of the string and pass the value to a function as a string.
char* s="abcd";
int i = strlen(s);
char* lengthAsString = ????(i);
char* s = "abcd";
int i = strlen(s);
char lengthAsString[50];
sprintf(lengthAsString, "%d", i);
// now you can use lengthAsString to pass it to a function