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
Related
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
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:
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:
function returns address of local variable in c [duplicate]
(1 answer)
Returning a C string from a function
(15 answers)
Closed 4 years ago.
I try to create a function, which returns astring (char array);
I tried different ways, but I always got error like:
[Error] conflicting types for 'readCommand'
Code:
char* cmd; //char cmd[256]
*cmd = readCommand();
//function
char* readCommand()
{
char cmd[256];
fgets(cmd, sizeof(cmd), stdin);
return cmd;
}
How can I return a string from this function?
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.