Reputation:
I am trying to write a C program code to copy a string using the concept of pointers and functions. (I am aware of the strcpy() function but this is a practice problem for me). The code is given below:
#include<stdio.h>
#include<string.h>
char *copy_str(char *s);
void main()
{
char a[10] = "Human", *ptra;
ptra = a;
puts(copy_str(ptra));
}
char *copy_str(char *s){
char b[10],*ptrb;
ptrb = b;
int i=0;
while (*s != '\0'){
*(ptrb+i) = *s;
i++;
s++;
}
*(ptrb+i) = '\0';
return ptrb;
}
In the function, I am getting ptrb as "Human",but while passing it back I am getting an error. Please help! Thanks.
Upvotes: 0
Views: 67
Reputation: 126
The "error" you are referring to is lack of any output. It's useful to note that in the future you should post, verbatim, the nature of your error messages. What may seem insignificant to you may, in fact, be very important, and it makes helping you much, much faster.
Back to the problem, in the code:
char a[10] = "Human", *ptra;
ptra = a;
everything is dandy; you are pointing ptra
at the first element of the array a
and nothing bad is being attempted.
However, in your function, you are doing:
char *copy_str(char *s){
char b[10],*ptrb;
ptrb = b;
int i=0;
while (*s != '\0'){
*(ptrb+i) = *s;
i++;
s++;
}
*(ptrb+i) = '\0';
return ptrb;
}
which basically is copying the characters from the a
array to the b
array.
Now the problem is that the array ptrb
is pointing at is declared as char b[10]
, and since this is not an object with static storage duration its lifetime ends at the end of the block.
It is undefined behavior to refer to an object after its lifetime has ended.
Notice, by making one simple change:
static char b[10],*ptrb;
everything works just as expected. This is because objects with static storage duration are objects whose, according to the C standard, "lifetime is the entire execution of the program." [ISO-IEC 9899-2011 - 6.2.4p3]
So in the future, be sure to keep track of storage durations, which is a distinct topic different from (though of equal importance to) other object traits, such as lexical scope and linkage.
Upvotes: 0
Reputation: 30926
The array b
is freed when that scope is over.(local variable deallocated) You need to allocate some memory and assign it to ptrb
to get the correct result.
Rather do this:-
char *b = malloc(10*sizeof(char));
and then do the necessary operation. Here basically puts
blow up as it is given as argument a freed memory resulting in undefined behavior.
Note: It is needed to de-allocate memory allocated using malloc. Otherwise you may unwillingly create a memory leak.
10
doesn't mean anything special here. It is just the amount of memory needed which is determined by the the length of the string we need to copy.
We can simply allocate strlen(s)+1
and then we will allocate it. And then copy operation will be done.
Upvotes: 2
Reputation: 69
this happens because array b allocated inside the function, you need to initialize array b outside the function copy_str.
Upvotes: 0