473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How do I pass the & instead of a ptr?

#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]);
}
}
Nov 14 '05 #1
8 1125
Profetas wrote:

#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]);
}
}


--
pete
Nov 14 '05 #2
Profetas <xu*****@yahoo.com> wrote:

In addition, you don't allocate space for string.
int main(void)
{
char *string; char string[100];
printf( "Enter a line of text:\n" );
scanf("%s", &string);

scanf("%s", string);

--
Stan Tobias
sed 's/[A-Z]//g' to email
Nov 14 '05 #3
pete wrote:
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]);
}
}


Nov 14 '05 #4
[snips]

On Mon, 27 Sep 2004 12:08:30 +0000, pete wrote:
Profetas wrote:

#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);


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. :)
Nov 14 '05 #5
On Mon, 27 Sep 2004 07:22:46 -0400, "Profetas" <xu*****@yahoo.com>
wrote:
#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>>
Nov 14 '05 #6
Dave wrote:

pete wrote:
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.

--
pete
Nov 14 '05 #7
Dave <re*************@elcaro.moc> writes:
pete wrote:
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()'.
Nov 14 '05 #8
Kelsey Bjarnason wrote:

[snips]

On Mon, 27 Sep 2004 12:08:30 +0000, pete wrote:
Profetas wrote:

#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);


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.

--
pete
Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Collin VanDyck | last post by:
I have a basic understanding of this, so forgive me if I am overly simplistic in my explanation of my problem.. I am trying to get a Java/Xalan transform to pass through a numeric character...
12
by: ~~~ .NET Ed ~~~ | last post by:
Hi, I have a standalone XML file (with the appropriate xml document header) that works fine when I load it using XmlDocument. I can have child elements like this without problems: ...
5
by: MLH | last post by:
With Access 2.0, it was simple. 97 doesn't seem so obvious. Where do I start. I launched c:\windows\system32\Wrkgadm.exe and joined system.mdw in that same dir. Upon launching Access 97, I...
27
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get...
8
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"...
4
by: _Mario.lat | last post by:
Hallo, I have a little question: In the function session_set_save_handler I can pass the name of function which deal with session. In Xoops code I see the use of this function like that: ...
1
by: Tarlanim | last post by:
Hi, i'm looking for a way to pass parameter from a HTML-File to a XML-File. In HTML-File i have a reference to XML-File and i want to use the parametre from the HTML-File in XML-File (Parameter...
7
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get...
3
by: Tony | last post by:
I see that many pages have &amp; in querystring instead &. What is difference? Can I put page link (url) www.mysite.com/mypage.aspx?lang=EN&ID=15 or I need to write...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.