How do I pass the & instead of a ptr?

P

Profetas

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
void reverse(char *str);

int main(void)
{
char *string;

printf( "Enter a line of text:\n" );
scanf("%s", &string);

printf( "The string printed backwards is: " );
reverse(&string);
puts("\0");
exit(EXIT_SUCCESS);
}

void reverse(char *str)
{
if( str[0]=='\0')
{
return;
}
else
{
reverse(&str[1]);
putchar(str[0]);
}
}
 
P

pete

Profetas said:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
void reverse(char *str);

int main(void)
{
char *string;

printf( "Enter a line of text:\n" );
scanf("%s", &string);

printf( "The string printed backwards is: " );
reverse(&string);
reverse(string);

puts("\0");
exit(EXIT_SUCCESS);
}

void reverse(char *str)
{
if( str[0]=='\0')
{
return;
}
else
{

The following isn't going to work without major revision.
reverse(&str[1]);
putchar(str[0]);
}
}
 
K

Kelsey Bjarnason

[snips]

reverse(string);

Funny that you pointed this out, but not the fact that the scanf is hooped
- twice. Once by the fact it's using &, and again by the fact there's no
space reserved for the string. :)
 
B

Barry Schwarz

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
void reverse(char *str);

int main(void)
{
char *string;

printf( "Enter a line of text:\n" );
scanf("%s", &string);

This will cause scanf to store the data it reads into string itself,
not into any area string might point to. I'm pretty sure this is not
what you intended.
printf( "The string printed backwards is: " );
reverse(&string);

Your prototype says reverse takes a char*. &string is a char**. Your
compiler should produce a diagnostic for this.
puts("\0");
exit(EXIT_SUCCESS);
}

void reverse(char *str)
{
if( str[0]=='\0')

Learn to indent consistently. It will save you a lot of aggravation
later.
{
return;
}
else

The else is superfluous.
{
reverse(&str[1]);
putchar(str[0]);
}
}

Your question makes no sense and many readers will not even see it.
Include a cogent statement of what problem you need help with in the
body of your message.


<<Remove the del for email>>
 
P

pete

Dave said:
void reverse(char *str)
{
if( str[0]=='\0')
{
return;
}
else
{


The following isn't going to work without major revision.

Um, no, it's fine (if a slightly cack-handed way of doing it, using
tail-recursion instead of a simple iteration
- your confusion indicating
perfectly why it's cack-handed).

Thank you.
 
T

Tim Rentsch

Dave said:
pete said:
void reverse(char *str)
{
if( str[0]=='\0')
{
return;
}
else
{


The following isn't going to work without major revision.

Um, no, it's fine (if a slightly cack-handed way of doing it, using
tail-recursion instead of a simple iteration - your confusion indicating
perfectly why it's cack-handed).
reverse(&str[1]);
putchar(str[0]);
}
}

Recursion, yes. But not tail recursion. There is a 'putchar()' after
the call to 'reverse()'.
 
P

pete

Kelsey said:
[snips]

reverse(string);

Funny that you pointed this out,
but not the fact that the scanf is hooped
- twice. Once by the fact it's using &,
and again by the fact there's no space reserved for the string. :)

OK. Stop laughing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top