How to return a character pointer? - c

I'm new to C.
I'm trying to return a character pointer >> A pointer that points to a single character value.
I know I can simply return a character but I want to learn how to return a character pointer pointing to a single character value.
char * returnPointerToCharacter(){
char s = 's';
char * pointerToS = &s;
return pointerToS;
}
int main()
{
// This code below works
char h = 'h';
char * pointerToH = &h;
printf("%c \n", *pointerToH);
// This code below doesn't work
char * pointerToS = returnPointerToCharacter();
printf("%c \n", *pointerToS);
return 0;
}

The problem is that char s is on the stack, and gets popped from the stack, so you're returning a pointer to a destructed element.
If you just want a function that returns a char pointer, you could try something simple:
char * returnPointerToCharacter(char *s){
return s;
}
...// Do stuff
char f;
char * pointerToS = returnPointerToCharacter(&f);

I'm trying to return a character pointer
Your code does return a character pointer. But it also commits a cardinal sin: it returns a pointer to an object whose lifetime ends with the completion of the function call. The resulting pointer is therefore useless to caller.
There are several alternatives. Often, if one is going to return a pointer, one dynamically allocates the object to which it is to point. Dynamically allocated objects live until they are explicitly freed. For your particular purposes, however, I would suggest making the function's local variable static, which exactly means that it lives and retains its last-set value for the entire life of the program:
char * returnPointerToCharacter(){
static char s = 's';
return &s;
}

Related

C Why does passing first address of array to char pointer provide whole string?

I'm currently playing around with passing char array to a char pointer. Lots of the examples I see show how you need to allocate the memory that the string pointer will use before copying the char array to it. And when copying, you iterate through the array to store each address into the allocated char pointer.
In the example below I don't initialize the char pointer and neither do iterate through the array. I just pass the pointer of the first element.
int main()
{
char c[10] = "something";
// char* new_c = (char *)malloc(strlen(c)+1);
char *new_c = NULL;
new_c = c;
printf("%s", new_c);
return 0;
}
Why is new_c still printing the entire string? Why do people even bother iterating through the entire array to copy?
Run this program and you will get a clear picture of what is happening
#include <stdio.h>
#include <string.h>
int main(void) {
char c[10] = "something";
char *new_c = NULL;
char new_c_2[10] = "";
new_c = c; // copies address of 'c' to 'new_c'
for(int i=0; c[i]!='\0'; i++) {
new_c_2[i] = c[i]; // copies value of 'c' to 'new_c_2'
}
// Data before changing the value of 'c'
printf("\nData before changing the value of \'c\'\n");
printf("new_c = %s\n", new_c);
printf("new_c_2 = %s\n", new_c_2);
strcpy(c, "changed");
// Data after changing the value of 'c'
printf("\nData after changing the value of \'c\'\n");
printf("new_c = %s\n", new_c);
printf("new_c_2 = %s\n", new_c_2);
return 0;
}
OUTPUT:
Data before changing the value of 'c'
new_c = something
new_c_2 = something
Data after changing the value of 'c'
new_c = changed
new_c_2 = something
char *new_c = NULL;
new_c = c;
These statements are just pointing 'new_c' to the address of 'c'.
So, if you change the value of 'c', and use 'new_c' it will go to the address of 'c' and give the updated value.
We copy the string into another so that we can use the old value even if we change the value of 'c'.
Please refer to call by value and call by reference in C programming for further details.
You say "I don't initialize the char pointer". You do, with
new_c = c;
The array c decays to a pointer, just as if you had passed c directly to printf. And then it is printf which iterates through the array until the zero terminator is found.
Why do people even bother iterating through the entire array to copy?
well, maybe to be sure the original reference isn't deleted/overwritten after a while.
For instance, if the original buffer is a local/automatic variable, which will be invalid after returning from the function.
Or if I just do c[0] = '\0';. new_c doesn't display a thing now because I just killed the only copy of the string.
Both copy & referencing another string through a "shallow" pointer are useful, depending on the use cases.

Why can I return `int` after a function but not `char *`?

I'm a newbie to C. I had extended the question from the previous question: Strange behavior when returning "string" with C (Thanks for all who answered or commented that question, by the way.)
Pretty straight forward:
Why can this work:
#include <stdio.h>
int func() {
int i = 5;
return i;
}
int main() {
printf("%d",func());
}
But not this:
#include <stdio.h>
char * func() {
char c[] = "Hey there!";
return c;
}
int main() {
printf("%s",func());
}
From the previous question, logically the int i should not exist too because the function has returned, but why can it still be returned while char c[] cannot?
(It seems to be duplicated from "Pointers and memory scope" but I would like to know more about what is the difference between returning an int and a char *.)
Problem is not returning char *, it is returning something that is allocated on stack.
If you allocate memory for your string rather than pointing to function stack, there will be no problem. Something like this:
char * func() {
char c[] = "Hey there!";
return strdup(c);
}
int main() {
char* str = func();
printf("%s", str);
free(str);
}
It is important to mention that in both cases, you are copying a value and in both cases copied value is correct, but the meaning of copied value differs.
In first case, your are copying an int value and after your return from function, you are using that int value which will be valid. But in 2nd case, even though you have a valid pointer value, it refers to an invalid address of memory which is stack of called function.
Based on suggestions in comment, I decided to add another better practice in memory allocating for this code:
#define NULL (void*)0
int func(char *buf, int len) {
char c[] = "Hey there!";
int size = strlen(c) + 1;
if (len >= size) {
strcpy(buf, c);
}
return size;
}
int main() {
int size = func(NULL, 0);
char *buf = calloc(size, sizeof(*buf));
func(buf, size);
printf("%s", buf);
free(buf);
return 0;
}
Similar approach is used in a lot of windows API functions. This approach is better, because owner of pointer is more obvious (main in here).
In the first example the return value is copied. In your second example you're returning a pointer, which will point to a memory location which no longer exists.
In the first case, you return the int value 5 from the function. You can then print that value.
In the second case however, you return a value of type char *. That value points to an array that is local to the function func. After that function returns the array goes out of scope, so the pointer points to invalid memory.
The difference between these two cases is a value that you use directly, versus a pointer value that no longer points to valid memory. Had you returned a pointer to memory allocated by malloc, then the pointer would point to valid memory.
You are trying to return pointer to local array, which is very bad. If you want to return a pointer to array, allocate it dynamically using malloc inside your func();
Then you must call free() on caller side to free up memory you allocated when you no longer need it
In the first example, you return an int, and the second you return a pointer to a char. They both return in exactly the same manner, it is just a matter of understanding the stack and how values are returned.
Even though i was declared in the function and is allocated on the stack, when the function returns it returns the value of i (which is basically copied, so when i falls off the stack the value of i is still returned.)
This is the exact same thing that happens to the char * in the second example. It will still be a pointer to a char, and it returns the 'copied' value of c. However, since it was allocated on the stack, the address it points to is effectively invalid. The pointer value itself has not changed, but what it points to has.
You would have to dynamically allocate this to avoid this situation.
The return value of function is returned by copy. In the first example, you get a copy of the integer variable from the function. In the second you get a copy of the char pointer, not a copy of the string.
The pointer references the string data that has automatic storage, so is no longer valid after the function returns. The space becomes available for use by other code and many be modified - any attempt to access it has undefined behaviour.
The point is, it is a pointer that is returned, not a string; in C a strings (and more generally arrays) are not a first-class data types.
Depending on your needs there are a number of valid ways of returning the string data; for example the following is valid:
char* func()
{
static char c[] = "Hey there!";
return c;
}
because here although the local variable goes out of scope the static data is not destroyed or de-allocated, and any reference to it remains valid.
Another alternative is to embed the string in a struct which is a first-class data type:
typedef struct
{
char content[256] ;
} sString ;
sString func()
{
sString c = {"Hey there!"};
return c;
}
Or more conventionally to copy the data to a caller buffer:
char* func( char* buffer )
{
char c[] = "Hey there!";
strcpy( buffer, c ) ;
return buffer ;
}
I have omitted code to mitigate the possibility of buffer overrun above for clarity in this last example, such code is advised.

Using pointers to make a new string in function

I am doing a bit of studying about C pointers and how to transfer them to functions, so I made this program
#include <stdio.h>
char* my_copy(pnt);
void main()
{
int i;
char a[30];
char* p,*pnt;
printf("Please enter a string\n");
gets(a);
pnt = a;
p = my_copy(pnt);
for (i = 0; i < 2; i++)
printf("%c", p[i]);
}
char* my_copy(char* pnt)
{
char b[3];
char* g;
g = pnt;
b[0] = *pnt;
for (; *pnt != 0; pnt++);
pnt--;
b[1] = *pnt;
b[2] = NULL;
return b;
}
It's supposed to take a string using only pointers and send a pointer of the string to the function my_copy and return a pointer to a new string which contains the first and the last letter of the new string. Now the problem is that the p value does receive the 2 letters but I can't seem to print them. Does anyone have an idea why?
I see five issues with your code:
char* my_copy(pnt); is wrong. A function prototype specifies the types of the parameters, not their names. It should be char *my_copy(char *).
void main() is wrong. main should return int (and a parameterless function is specified as (void) in C): int main(void).
gets(a); is wrong. Any use of gets is a bug (buffer overflow) and gets itself has been removed from the standard library. Use fgets instead.
b[2] = NULL; is a type error. NULL is a pointer, but b[2] is a char. You want b[2] = '\0'; instead.
my_copy returns the address of a local variable (b). By the time the function returns, the variable is gone and the pointer is invalid. To fix this, you can have the caller specify another pointer (which tells my_copy where to store the result, like strcpy or fgets). You can also make the function return dynamically allocated memory, which the caller then has to free after it is done using it (like fopen / fclose).
You're returning an array from my_copy that you declared within the function. This was allocated on the stack and so is invalid when the function returns.
You need to allocate the new string on the heap:
#include <stdlib.h>
b = malloc(3);
if (b) {
/* Do your funny copy here */
}
Don't forget to free() the returned string when you've finished with it.

This program is giving output "abc" for both p and c, but how?

When pc is assigning cdefg, why it is printing abc. when it goes to fun it is assigning pc= ""cdefg"
void fun(char *pc)
{
pc = malloc(5);
pc = "cdefg";
}
int main()
{
char *p = "abc";
char *c = p;
fun(p);
printf("%s %s\n",p,c);
}
The reason your program does what it does is that the assignment of pc in fun has nothing to do with assigning p in main. The pointer is passed by value; any changes made by fun get discarded.
If you would like to assign a new value inside a function, do one of three things:
Pass a pointer to pointer, or
Allocate a buffer in the caller, and pass it to the function, along with buffer's length, or
Return the pointer from the function, and assign in the caller.
First approach:
void fun(char **ppc) {
*ppc = "cdefg";
}
...
fun(&p); // in main
Second approach:
void fun(char *pc, size_t len) {
if (len >= 6) {
strcpy(pc, "cdefg");
}
}
...
char p[20]; // in main
fun(p, 20);
Third approach:
char *fun() {
return "sdefg";
}
...
char *p = fun(); // in main
Your program has other issues - for example, malloc-ed memory gets leaked by the assignment that follows.
Try this instead. It actually updates the original pointer, rather than assigning to a copy which is then left dangling:
void fun(char **pc)
{
*pc = malloc(6);
strcpy(*pc, "cdefg");
}
int main()
{
char *p = "abc";
char *c = p;
fun(&p);
printf("%s %s\n",p,c);
}
It also fixed 2 other problems. The buffer of size 5 isn't big enough for the string plus the string terminator character, and you also need to copy the string into the buffer - assignment won't work.
When the function fun is called, the value of the pointer p is copied. Thus, only the local pointer pc in fun is changed. If you want to change the value of a pointer, you should take a double pointer as argument.
By the way, you do not have to call malloc(3) because the string "cdefg" is already present in memory (in rodata). The instruction pc = "cdefg"; puts the address of "cdefg" into pc. You will loose the address of the memory allocated by malloc(3), it's a memory leak.
When you allocated the pointer again in caller function, the value of pointer variable changed. In order to take this new value to the calling function, you have to pass the address of the pointer. ie: pass the pointer by reference.
There are two things at play here, passing by value and reassigning instead of copying.
If we start with the simple reassignment, take a closer look at these two lines:
pc = malloc(5);
pc = "cdefg";
The first lines assign to pc, making pc point to whatever memory malloc returned. The second line reassigns pc to point somewhere else. These two lines are basically the same as having an int variable i and doing
i = 1;
i = 2;
The first assignment you do is lost because the you immediately make another assignment. To make the memory returned by malloc contain the string "cdefg" there are two things you need to do: The first is that you need to allocate six characters, to fit the string terminator, and the second thing you need to do is to copy the string into the memory:
pc = malloc(strlen("cdefg") + 1);
strcpy(pc, "cdefg");
The second issue is more complex, and has to do with how arguments are passed in C. In C the arguments are passed by values which means they are copied and the function only have a local copy of the data in those arguments.
When you pass a pointer, like in your code, then the pointer is copied into the variable pc, and when the function returns the variable goes out of scope and all changes you made to the variable (like reassigning it to point somewhere else) are simply lost.
The solution is to pass arguments by reference. This is unfortunately not possible in C, but it can be emulated using pointers, or rather using pointers to variables. To pass a pointer to a variable that is a pointer, the type is a pointer to a pointer to some other type, so the function should take a pointer to a pointer to char:
void fun(char **ppc) { ... }
The variable ppc points to the variable p from the main function.
Now since ppc is pointing to a variable, you need to use the dereference operator to access the original variable:
*ppc = malloc(strlen("cdefg") + 1);
strcpy(*ppc, "cdefg");
To call the function you use the address-of operator & to create a pointer to the variable:
char *p = "abc";
...
fun(&p);
Because
char *p - in main function
and
char *pc - in fun function
are different variables.

Pointers and Strings in C

I'm trying to write a functions to modify strings in C.
If I have a function like
char *func(char *s){
char *t=s;
s++; //option 1
t++; //option 2
*t='a'; //option 2
return s;
}
If I do something like [option 1]: s++; I believe it will return the pointer to wherever s is now pointing. If I do [option 2]: t++; and *t='a'; then return s, will it return the address for the first spot of s but with modified contents or will it return the address with the original content?
char *func(char *s)
In this code, s is a pointer to a region of memory, that (I assume) represents a string.
s++;
now s points to the next char, in the same region of memory, that (I assume) represents a string.
char *t=s;
Now you have two pointers to that region of memory.
t++;
*t='a';
Now you have changed that region of memory, replacing the second character with an 'a'.
Therefore: if you then return s, you will return a pointer to that same region of memory, which was altered. If you want to return an altered copy of the string, you have to make a copy of the memory first.
char *func(char *s){
char *t=strdup(s); //duplicates a string
s++;
*t='a';
free(t); //you have to free the memory from strdup at some point
return s; //returns pointer to second character of unchanged string
}
If I do t++; and *t='a'; then return s, will it return the address for
the first spot of s but with modified contents or will it return the
address with the original content?
I believe your question assumes something like this:
char s[] = "abcde";
char *t = s;
t++;
*t = 'a';
Here you've got one string and two pointers that initially both point to the beginning of the string. At the third line, you modify one of the pointers to point to the next character. At the fourth line, you modify the data at that location. Since s points to the same data, and the data is modified, the string that s points to will change.
printf("s is: %s\n", s"); // s is: aacde
printf("t is: %s\n", t"); // t is: acde
BTW, there's no better way to really learn this stuff than to write little test programs and play with them. A good book will explain how things are supposed to work, but playing with code is the way that you grow to really understand the code and believe what the books tell you.
In the code:
char *func(char *s)
{
char *t=s;
}
you are missing a return statement, so garbage is returned.
The variable s is a local copy of whatever you passed to the function. If you write s++ inside the function, you change what the local pointer points at, but don't change the argument.
When you do t++; and *t = 'a';, you are making t point to the second character of the string passed as s, and then assigning the character 'a' to that.
So, if you have:
char *func(char *s)
{
char *t = s;
s++;
t++;
*t = 'a';
return s;
}
then the return value will be a pointer to the second character of the original string, which has been modified to contain 'a'.
What behaviour do you need?
Both returning the initial value of s, and returning the final value of s make sense.
The one option you do not have is doing *t='a' but returning with the original memory unchanged. Memory is changed
Returning the original s is quite common, for example strcpy.
It is also common to return a pointer to 'the next char after the one I just modified' (which is an option you did not offer).
You need to think through, and decide what is the helpful thing for the function to do?

Resources