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
- What exactly is the difference between “pass by reference” in C and in C++?
- What does “collect2: error: ld returned 1 exit status” mean?
- Floating point exception (core dumped)
- Correct format specifier for double in printf
- What is the behavior of integer division?
- Error “initializer element is not constant” when trying to initialize variable with const
- Cannot assign requested address – possible causes?
- need help understanding the movzbl call in this function
- What is the difference between read and pread in unix?
- What does “control reaches end of non-void function” mean?
- C dynamically growing array
- Why do I get an assertion failure?
- Difference between int32, int, int32_t, int8 and int8_t
- C compile error: “Variable-sized object may not be initialized”
- What does WEXITSTATUS(status) return?
- Difference between a Structure and a Union
- Xcode – Warning: Implicit declaration of function is invalid in C99
- Error: control may reach end of non-void function in C
- return makes integer from pointer without a cast [-Wint-conversion] return candidate
- Math constant PI value in C
- How can I use an array of function pointers?
- Convert String to int array in java
- How can one print a size_t variable portably using the printf family?
- How to make sense of modulo in c
- How to sort an array of objects in Java?
- Makefile:1: *** missing separator. Stop
- Comparing the values of char arrays in C++
- xorl %eax – Instruction set architecture in IA-32
- Trim a string in C [duplicate]
- Removing last character in C
- Difference between char *argv[] and char **argv for the second argument to main()
- Dereference void pointer
- Use of flag in c?
- GDB no such file or directory
- variably modified array at file scope in C
- Remove empty array elements