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 [duplicate]
- 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 to use execvp()
- How to use execvp() to execute a command
- what is Segmentation fault (core dumped)? [duplicate]
- How to print in C
- What is newline character — ‘\n’
- Why does ENOENT mean “No such file or directory”?
- What does the question mark character (‘?’) mean?
- What is Bit Masking?
- How do I use extern to share variables between source files?
- Cannot assign requested address – possible causes?
- error: called object is not a function or function pointer
- munmap_chunk(): invalid pointer
- How to convert a std::string to const char* or char*
- No Symbol Table using GDB on Compiled Programs
- What is char ** in C?
- Get text from user input using C
- warning: expression result unused
- error: indirection requires pointer operand (‘int’ invalid)
- What is the difference between exit(0) and exit(1) in C?
- Constant pointer vs Pointer to constant
- Variable warning set but not used
- Expected declaration specifier error in function
- How to initialize array to 0 in C?
- strcmp not working
- C# – How to convert string to char?
- error: aggregate value used where an integer was expected
- How can I find the number of elements in an array?
- Parameter name omitted error?
- Return a `struct` from a function in C
- C: scanf to array
- What are “prototypes” in a C program?
- What’s the difference between “mod” and “remainder”?
- undefined reference to `std::ios_base::Init::Init()’
- previous declaration of ‘function’ was here in C [duplicate]
- How to Compare 2 Character Arrays [duplicate]
- Convert an array to string