Notice you’re not dynamically allocating the variable, which pretty much means the data inside str
, in your function, will be lost by the end of the function.
You should have:
char * createStr() { char char1= 'm'; char char2= 'y'; char *str = malloc(3); str[0] = char1; str[1] = char2; str[2] = '\0'; return str; }
Then, when you call the function, the type of the variable that will receive the data must match that of the function return. So, you should have:
char *returned_str = createStr();
It worths mentioning that the returned value must be freed to prevent memory leaks.
char *returned_str = createStr(); //doSomething ... free(returned_str);
Related Posts:
- Return char[]/string from a function
- The difference between char * and char[] [duplicate]
- char *array and char array[]
- How do I create an array of strings in C?
- What is the difference between char s[] and char *s?
- Returning string from C function
- How do I create an array of strings in C?
- How to print a char array in C through printf?
- How to clear all the elements of array in C?
- Excess elements in char array initializer error
- Does C have a string type?
- How do I check if a string contains a certain character?
- How does strtok() split the string into tokens in C?
- How to convert an int to string in C?
- What is a string of hexadecimal digits?
- How to convert an int to string in C?
- How do I determine the size of my array in C?
- How to convert a string to integer in C?
- Get a substring of a char* [duplicate]
- Get a substring of a char*
- How to convert a char array to a string?
- Expression must be a modifiable L-value
- “Expected expression before ‘ { ‘ token”
- C pointers and arrays: [Warning] assignment makes pointer from integer without a cast
- How to print the array?
- “error: assignment to expression with array type error” when I assign a struct field (C)
- How do I properly compare strings in C?
- How do you make an array of structs in C?
- How to convert an int to string in C?
- Char Comparison in C
- How to check if a string is a number?
- Why am I getting “array initializer must be an initializer list or string literal”?
- Using Dynamic Memory allocation for arrays
- How do I lowercase a string in C?
- How do I create an array of strings in C?
- Finding the length of a Character Array in C
- How do I concatenate const/literal strings in C?
- Reversing a string in C
- How to remove the character at a given index from a string in C?
- Zero an array in C code
- Passing an array by reference in C?
- Split string with delimiters in C
- Warning: return from incompatible pointer type in C
- How to create an array of strings in C?
- I’m getting “Invalid Initializer”, what am I doing wrong?
- C char* to int conversion
- Convert char array to a int number in C
- Returning an array using C
- Convert char array to string use C
- Iterate through a C array
- char pointers: invalid conversion from ‘char*’ to ‘char’?
- Allocating string with malloc
- What is the difference between char array and char pointer in C?
- Removing trailing newline character from fgets() input
- How to copy a char array in C?
- How to empty a char array?
- Array type char[] is not assignable
- Allocating char array using malloc
- error: function returns address of local variable
- C char array initialization
- How to concatenate string and int in C?
- char array not assignable
- C: warning: excess elements in array initializer; near initialization for ‘xxx’ ; expects ‘char *’, but has type ‘int’
- Convert Char to String in C
- How big can a 64 bit unsigned integer be?
- The difference between n++ and ++n at the end of a while loop? (ANSI C)
- Implementing Taylor Series for sine and cosine in C
- Working on code to calculate cosine with factorial sum
- How to use execvp()
- How to use execvp() to execute a command
- warning: implicit declaration of function
- warning: implicit declaration of function
- What is *(uint32_t*)?
- pthread_join() and pthread_exit()
- what is Segmentation fault (core dumped)? [duplicate]
- What causes a segmentation fault (core dump) to occur in C?
- uint8_t vs unsigned char
- How to use symbols of extended ASCII table in C?
- Two questions about basic C programs
- What is the difference between ++i and i++?
- Using boolean values in C
- std::string to char*
- What does “collect2: error: ld returned 1 exit status” mean?
- typedef struct vs struct definitions [duplicate]
- How to print in C
- How to convert a char to a String?
- Why should we typedef a struct so often in C?
- How to convert/parse from String to char in java?
- Why should we typedef a struct so often in C?
- What exactly is the difference between “pass by reference” in C and in C++?
- What is the difference between ++i and i++?
- Conversion from string to char – c++
- What can be the reasons of connection refused errors?
- What does “collect2: error: ld returned 1 exit status” mean?
- typedef struct vs struct definitions [duplicate]
- Array to String PHP?
- What is newline character — ‘\n’
- Why does ENOENT mean “No such file or directory”?
- What does the question mark character (‘?’) mean?
- Cannot figure out how to use getchar(); in C