Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Visual recursion

Reply
Thread Tools

Visual recursion

 
 
rrice899@gmail.com
Guest
Posts: n/a
 
      07-10-2013
On Tuesday, July 9, 2013 11:11:54 AM UTC-7, James Kuyper wrote:
> On 07/09/2013 01:36 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> > Hi,

>
> > I want to look at how recursion works. So I modified another recursion file with some code to print a dash ('-') starting from an empty char pointer "*s". Then concatenating the dash to 's'. But I can't get it to print or even run. There is one error:

>
> > line 11 error: expected ')' before string constant.

>
> > This is my first post and I'm just learning c(teaching myself). I apologize for any transgressions. I appreciate any comments. Below is the program:

>
> >

>
> > #include <stdio.h>

>
> > #include <string.h>

>
> > void towers(int, char, char, char);

>
> > void towers(int n, char frompeg, char topeg, char auxpeg){

>
> > int indent = 10;

>
> > int i;

>
> > char *str = "";

>
>
>
> This code causes the creation of an array of char of length 1,
>
> containing '\0'. It also sets str to point at the first (and only)
>
> element of that array. Your program has undefined behavior if it makes
>
> any attempt to write to that array.
>
>
>
> > {

>
> > for(i = 0; i < indent; i++)

>
> > {

>
> > char *strcat(str, "-");

>
>
>
> This is the line that the compiler was complaining about. It's just
>
> plain wrong, but it's so wrong that I'm having a hard time deciding how
>
> to to describe what's wrong with it. Your compiler had trouble too - the
>
> error message it produced doesn't make things any clearer.
>
>
>
> The 'char *' portion only makes sense if you are writing a declaration.
>
> You don't need to declare strcat(), because it is declared in
>
> <string.h>. You've already #included <string.h>, so you shouldn't
>
> re-declare it.
>
> On the other hand, the (str, "-") part looks like you're trying to call
>
> strcat(), rather than declare it.
>
>
>
> It's possible to declare something on the same line that calls a
>
> function, but that isn't the way to do so. My best guess is that you
>
> intended to write something like this:
>
>
>
> char *q = strcat(str, "-");
>
>
>
> However, there's no need to save the value returned by a function in a
>
> variable, unless you intend to do something with that value. It's
>
> perfectly acceptable to write
>
>
>
> strcat(str, "-");
>
>
>
> except for the following issue: the first time it's called, strcat()
>
> will attempt to write '-' in the place pointed at by str. Such a write
>
> has undefined behavior, because str points at the array allocated to
>
> store a string literal. Even if that array were writable, you'd still
>
> have problems, because the second thing that strcat() will try to do is
>
> write a '\0' in the position one past the end of that array, which also
>
> has undefined behavior.
>
>
>
> What you need to do in cases like this is to allocate an actual buffer
>
> that is guaranteed to be long enough (and determining how long it needs
>
> to be can be a problem in cases like this):
>
>
>
> #define LONG_ENOUGH 256
>
> char str[LONG_ENOUGH] = "";
>
>
>
> This line creates a writable array named str, of length LONG_ENOUGH, and
>
> initialized it with all zeros. The first zero is copied from the string
>
> literal. Because one element of the array was initialized explicitly,
>
> all of the other elements of the array are initialized to the default
>
> value, which happens to be zero, too.


Thank you very much James. Your suggestions helped a great deal. The output now prints dashes of various lengths for each recursion call.
To Keith regarding indenting: I will follow your directions.
I assure everyone this is not homework. I'm learning C from a book and an IDE called Code::Blocks.
My corrected code is below with James' suggestions on line 4 and 11.
#include <stdio.h>
#include <string.h>
#define LONG_ENOUGH 256
char str[LONG_ENOUGH]; //James Kuyper
void towers(int, char, char, char);
void towers(int n, char frompeg, char topeg, char auxpeg){
int indent = 10;
int i;
{
for(i = 0; i < indent; i++)
strcat(str, "-"); //James Kuyper
if(n > 0){
towers(n-1, frompeg, auxpeg, topeg);
printf("\nMove disk %d from peg %c to peg %c\n", n, frompeg, topeg);
printf("%s\n", str);
towers(n-1, auxpeg, topeg, frompeg);
}
}
}
int main(){
int n = 2;
printf("The tower of Hanoi involves the moves: \n\n");
towers(n, 'A', 'C', 'B');
return 0;
}
OUTPUT
The tower of Hanoi involves the moves:


Move disk 1 from peg A to peg B
------------------------------

Move disk 2 from peg A to peg C
----------------------------------------

Move disk 1 from peg B to peg C
------------------------------------------------------------

Process returned 0 (0x0) execution time : 0.085 s
Press any key to continue.

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      07-10-2013
(E-Mail Removed) writes:
[snip]
> Thank you very much James. Your suggestions helped a great deal. The output now prints dashes of various lengths for each recursion call.
> To Keith regarding indenting: I will follow your directions.


My suggest was that you (a) keep your lines of text shorter than 80
columns, and (b) properly indent your C code. You're still doing
neither.

> I assure everyone this is not homework. I'm learning C from a book and an IDE called Code::Blocks.
> My corrected code is below with James' suggestions on line 4 and 11.
> #include <stdio.h>
> #include <string.h>
> #define LONG_ENOUGH 256
> char str[LONG_ENOUGH]; //James Kuyper
> void towers(int, char, char, char);
> void towers(int n, char frompeg, char topeg, char auxpeg){
> int indent = 10;
> int i;
> {
> for(i = 0; i < indent; i++)
> strcat(str, "-"); //James Kuyper
> if(n > 0){
> towers(n-1, frompeg, auxpeg, topeg);
> printf("\nMove disk %d from peg %c to peg %c\n", n, frompeg, topeg);
> printf("%s\n", str);
> towers(n-1, auxpeg, topeg, frompeg);
> }
> }
> }
> int main(){
> int n = 2;
> printf("The tower of Hanoi involves the moves: \n\n");
> towers(n, 'A', 'C', 'B');
> return 0;
> }

[...]

Sometimes if you use tab characters to indent your code, they can be
lost when posting to Usenet, depending on how you post. Or is your code
really left-justified like that?

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
va_arg... recursion: changing arguments and the using recursion jononanon@googlemail.com C Programming 8 04-26-2012 08:37 PM
visual studio .net 2003 verses visual studio .net 2002 wh ASP .Net 2 01-16-2004 04:54 PM
eMbedded Visual Basic & Visual Studio .NET - No Longer Supported? ¢harlie MCSD 0 12-26-2003 07:23 PM
Trivial question - Visual C# .Net vs Visual Studio .Net John Timbers ASP .Net 32 11-08-2003 06:12 AM
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET Jaime MCSD 2 09-20-2003 05:16 AM



Advertisments