Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am trying to reverse a string using pointers by making a function

What I have tried:

#include<stdio.h>
char reversestring(char* t);
int main(){
    char s[100],r[100];
    char* t;

    printf("enter the string");
    gets(s);
    t=s;
   r[100]=reversestring(t);
    puts(r);

}

now this is my funtion which i created to reverse the string can anyone tell me where is the error in this function
char reversestring(char* t)
{
    int count=0,end,begin;

    char r[100];

 while(*(t+count)!='\0')
    {
        count++;
}
     end=count-1;


     for(begin=0;begin<count;begin++){
     r[begin]=*(t+end);
     end--;
}

return r;
}

i m new to programming please help me
Posted
Updated 10-Jun-18 21:48pm
Comments
CHill60 11-Jun-18 3:16am    
What actually happens when you run/debug it?
kavinderrana121 11-Jun-18 3:19am    
it is showing some garbage value(weird symbols) as output when i am running this program

Look at your code.
char reversestring(char* t)
{
...
    char r[100];
...
return r;
}
And
    char s[100],r[100];
...
   r[100]=reversestring(t);
So what - exactly - is reversestring returning? Is it a char? No - it's an array of chars, which is a pointer-to-char.
And where do you store this? In a pointer-to-char? No, you try to put a pointer into an array at index 100, which is not even inside the array because C arrays start from index zero.

Start by correcting the code so reversestring returns the right type: pointer-to-char.
Then stop what you are doing and learn about dangling references - which is what returning r would be: it's a local array, so it only exists as long as your function is running. if your return a pointer to local memory, you will get problems later. So change your reversestring function to take two parameters: the input array and the output array, and then return the output array.

Finally, don't store the result in a single element of the array! Store it in a pointer-to-char variable instead!
 
Share this answer
 
Comments
[no name] 11-Jun-18 3:51am    
Oops you posted while I was still typing, but we basically have the same conclusion. :)
OriginalGriff 11-Jun-18 4:33am    
Happens all the time! :laugh:
Maciej Los 11-Jun-18 4:06am    
5ed!
There's a few things wrong:

- reversestring returns a single character rather than a char*
- the result is assigned to r[100] instead of to r
- r in reversestring is only on the stack within the function and cannot be returned like that

You can fix it for example like so:

#include <stdio.h>

void reversestring(char* input, char* output);

int main() {
	char s[100], t[100];

	printf("enter the string");
	gets(s);
	reversestring(s, t);
	puts(t);
}

void reversestring(char* input, char* output)
{
	int count = 0, begin;

	while (*(input + count) != '\0')
	{
		count++;
	}

	for (begin = 0; begin<count; begin++)
	{
		output[begin] = *(input + count - 1 - begin);
	}
	output[count] = '\0';
}


Note that you will have a buffer overflow if the entered string doesn't fit in s.
 
Share this answer
 
Comments
Maciej Los 11-Jun-18 4:06am    
5ed!
kavinderrana121 11-Jun-18 7:00am    
to avoid buffer overflow how will i use memory allocation using malloc for this problem
[no name] 11-Jun-18 7:02am    
You can use gets_s instead of gets to make sure the input fits in s[]. Malloc doesn't help with an overflow in this case.
kavinderrana121 11-Jun-18 7:05am    
but if i want to use malloc then i will use that in main function or reversestring function??
[no name] 11-Jun-18 7:07am    
In main:
char* s = (char*)malloc(100);
char* t = (char*)malloc(100);

The function just uses the provided buffers, it doesn't need to do any allocation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900