Extremely short -extremely complicated 
Author Message
 Extremely short -extremely complicated

Here are small programs that I'm trying to find errors in following
programms. I already took me hours of debugging and it seems I'm getting
nowhere. Please help.

/*program #1*/
int main()
   {
   char s[5][10];

   for (i = 0 ; i < 10 ; i++);
      strcpy(s[i], "Copying into string %d", i);

   for (i = 0 ; i < 10 ; i++)
      printf("\nString %d contains %c", s[i]);
   return(0);
   }

/*program #2*/
typedef struct Person
   {
   char *name, *address;
   long id;
   }

int main()
   {
   Person *Bob[2];
   int i;

   for (i = 0 ; i < 2 ; i++)
      Bob[i] = malloc(sizeof(Person));

   strcpy(Bob[1].name, "Bob Smith");
   strcpy(Bob[1].address, "1234 Main St.");
   Bob[1].id = 100;
   strcpy(Bob[2].name, "Bob Jones");
   strcpy(Bob[2].address, "1000 5th St.");
   Bob[2].id = 101;

   for (i = 0 ; i < 2 ; i++)
      printf("ID of Bob %d is %lf", i, Bob[i].id)
   return(0);
   }



Sat, 10 Nov 2001 03:00:00 GMT  
 Extremely short -extremely complicated

Quote:
>>>>>>>>>>>>>>>>>> Ursprngliche Nachricht <<<<<<<<<<<<<<<<<<


Extremely short -extremely complicated:

Quote:
> Here are small programs that I'm trying to find errors in following
> programms. I already took me hours of debugging and it seems I'm
getting
> nowhere. Please help.
> /*program #1*/
> int main()
>    {
>    char s[5][10];
>    for (i = 0 ; i < 10 ; i++);
>       strcpy(s[i], "Copying into string %d", i);

Here you have to use the second index not the first one to set
characters in the strings.
The loop is as follows:
        for( i=0 ;i < 5 ;i++ ) {     /* to access each string */
                for( j=0 ;j < 10 ;j++ )      /* to access the characters in the current
string */
                        strcpy(s[i][j], "Copying into string %d", i);
        }
The variable i selects string 1 to 5 and variable j selects the
characters 1 to 10 in string no. i.

Quote:
>    for (i = 0 ; i < 10 ; i++)
>       printf("\nString %d contains %c", s[i]);
>    return(0);
>    }
> /*program #2*/
> typedef struct Person
>    {
>    char *name, *address;
>    long id;
>    }

The typename is missing. The syntax is as follows
        typedef struct <struct-name> { <elements> } <typename>;

Quote:
> int main()
>    {
>    Person *Bob[2];
>    int i;
>    for (i = 0 ; i < 2 ; i++)
>       Bob[i] = malloc(sizeof(Person));

Here you've forgot to allocate space for the char pointers. The malloc
above will only request memory for the struct with it pointers (to
keep the address of an allocated memory block) and the long.
For the pointers you have to allocate memory explicitly !!

Quote:
>    strcpy(Bob[1].name, "Bob Smith");
>    strcpy(Bob[1].address, "1234 Main St.");
>    Bob[1].id = 100;
>    strcpy(Bob[2].name, "Bob Jones");
>    strcpy(Bob[2].address, "1000 5th St.");
>    Bob[2].id = 101;
>    for (i = 0 ; i < 2 ; i++)
>       printf("ID of Bob %d is %lf", i, Bob[i].id)
>    return(0);
>    }

Hope that helps :-)


Sat, 10 Nov 2001 03:00:00 GMT  
 Extremely short -extremely complicated


Quote:
> Here are small programs that I'm trying to find errors in following
> programms. I already took me hours of debugging and it seems I'm
getting
> nowhere. Please help.

> /*program #1*/
> int main()
>    {
>    char s[5][10];

>    for (i = 0 ; i < 10 ; i++);
>       strcpy(s[i], "Copying into string %d", i);

        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        This is completly WRONG!

Did you include string.h before using this function ?
the strcpy function has a prototype as
char *strcpy(char *s1,const char *s2);
The strcpy function copies the string pointed to by s2 (including the
terminating null character) into the array pointed to by s1

--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---



Sat, 10 Nov 2001 03:00:00 GMT  
 Extremely short -extremely complicated


Quote:
> Here are small programs that I'm trying to find errors in following
> programms. I already took me hours of debugging and it seems
> I'm getting nowhere. Please help.

   First and foremost - take a good tutorial and actually *read* it.
Quote:

> /*program #1*/

  #include <stdlib.h>
  #include <stdio.h>
  #include <string.h>

Quote:
> int main()
>    {
>    char s[5][10];

>    for (i = 0 ; i < 10 ; i++);
>       strcpy(s[i], "Copying into string %d", i);

 char *strcpy(char *s1, const char *s2);
The function copies the string s2, including its terminating null
character, to successive elements of the array of char whose first
element has the address s1. It returns s1.

  You need here
 int sprintf(char *s, const char *format, ...);

The function generates formatted text, under the control of the format
format and any additional arguments, and stores each generated character
in successive locations of the array object whose first element has
the address s. The function concludes by storing a null character
in the next location of the array. It returns the number of characters
generated -- not including the null character.

   But you are trying to stuff a rather long string into an array that
is too small for it.

Quote:

>    for (i = 0 ; i < 10 ; i++)
>       printf("\nString %d contains %c", s[i]);
>    return(0);
>    }

> /*program #2*/

  #include <stdlib.h>
  #include <stdio.h>
  #include <string.h>

Quote:
> typedef struct Person
>    {
>    char *name, *address;
>    long id;
>    }

> int main()
>    {
>    Person *Bob[2];
>    int i;

>    for (i = 0 ; i < 2 ; i++)
>       Bob[i] = malloc(sizeof(Person));

>    strcpy(Bob[1].name, "Bob Smith");

      Segmentation fault, code dumped. Pointer Bob[1].name
is not initialised, you haven't allocated any memory to store your
copied string.

Quote:
>    strcpy(Bob[1].address, "1234 Main St.");

      Ditto.

Quote:
>    Bob[1].id = 100;
>    strcpy(Bob[2].name, "Bob Jones");

     Ditto.

Quote:
>    strcpy(Bob[2].address, "1000 5th St.");

     Ditto.

Quote:
>    Bob[2].id = 101;

>    for (i = 0 ; i < 2 ; i++)
>       printf("ID of Bob %d is %lf", i, Bob[i].id)
>    return(0);
>    }

--
        Regards,
                Alex Krol
Disclaimer: I'm not speaking for Scitex Corporation Ltd

--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---



Sat, 10 Nov 2001 03:00:00 GMT  
 Extremely short -extremely complicated

Hello,

Am 25.05.99, 11:28:31, schrieb Hendrik Kuehne

-extremely complicated:

Quote:
> > /*program #1*/
> > int main()
> >    {
> >    char s[5][10];
> >    for (i = 0 ; i < 10 ; i++);
> >       strcpy(s[i], "Copying into string %d", i);
> Here you have to use the second index not the first one to set
> characters in the strings.
> The loop is as follows:
>       for( i=0 ;i < 5 ;i++ ) {        /* to access each string */
>               for( j=0 ;j < 10 ;j++ ) /* to access the characters in
the current
> string */
>                       strcpy(s[i][j], "Copying into string %d", i);
>       }
> The variable i selects string 1 to 5 and variable j selects the
> characters 1 to 10 in string no. i.

And additionally the argument of strcpy is way too long (more than 9
chars) for the array.

Greetings,
  Thomas



Sat, 10 Nov 2001 03:00:00 GMT  
 Extremely short -extremely complicated

# Hello,

# Am 25.05.99, 11:28:31, schrieb Hendrik Kuehne

# -extremely complicated:

#> > /*program #1*/
#> > int main()
#> >    {
#> >    char s[5][10];

#> >    for (i = 0 ; i < 10 ; i++);
    Note the superfluous semicolon^

#> >       strcpy(s[i], "Copying into string %d", i);

Totally confused. strcpy takes only two args, and doesn't
do %d conversion.

#> Here you have to use the second index not the first one to set
#> characters in the strings.
#> The loop is as follows:
#>       for( i=0 ;i < 5 ;i++ ) {        /* to access each string */
#>               for( j=0 ;j < 10 ;j++ ) /* to access the characters in
# the current
#> string */
#>                       strcpy(s[i][j], "Copying into string %d", i);

Do you know what you are suggesting? s[i][j] is a char, so we have
a conversion of char to pointer without a cast. Plus the already
mentioned bogosity.

#>       }
#> The variable i selects string 1 to 5 and variable j selects the
#> characters 1 to 10 in string no. i.

# And additionally the argument of strcpy is way too long (more than 9
# chars) for the array.

Utter bogosity. If you only had test-compiled your suggestion;
even with the lowest warning level you should have spotted a
number of errors.

clc rule of thumb: if you're not reasonably sure that your answer
is correct, don't post. You will only make a fool of yourself in
front of a million people. The usenet archives will conserve your
bogons forever.

Regards,

        Jens
--
Jens Schweikhardt  http://www.shuttle.de/schweikh/
SIGSIG -- signature too long (core dumped)



Sat, 10 Nov 2001 03:00:00 GMT  
 Extremely short -extremely complicated

Quote:

> Here are small programs that I'm trying to find errors in following
> programms. I already took me hours of debugging and it seems I'm getting
> nowhere. Please help.

You need to spend a lot of time rereading your C text.  Don't assume you can
fake your way through programming classes; it shouldn't work in English Lit,
either.  Notice the differences between your code and the following.

#include <stdio.h>

int program_1()
{
    char s[5][10];
    int i;
    for (i = 0; i < 10; i++)
        sprintf(s[i], "%d", i);
    for (i = 0; i < 10; i++)
        printf("String %d contains %s\n", i, s[i]);
    return 0;

Quote:
}

/*program #2 */
typedef struct {
    char name[12], address[16];
    long id;

Quote:
} Person;

int program_2()
{
    Person Bob[2];
    int i;

    strcpy(Bob[0].name, "Bob Smith");
    strcpy(Bob[0].address, "1234 Main St.");
    Bob[0].id = 100;
    strcpy(Bob[1].name, "Bob Jones");
    strcpy(Bob[1].address, "1000 5th St.");
    Bob[1].id = 101;

    for (i = 0; i < 2; i++)
        printf("ID of Bob %d is %ld\n", i, Bob[i].id);
    return 0;

Quote:
}

int main(void)
{
    program_1();
    program_2();
    return 0;

Quote:
}

> /*program #1*/
> int main()
>    {
>    char s[5][10];

>    for (i = 0 ; i < 10 ; i++);
>       strcpy(s[i], "Copying into string %d", i);

>    for (i = 0 ; i < 10 ; i++)
>       printf("\nString %d contains %c", s[i]);
>    return(0);
>    }

> /*program #2*/
> typedef struct Person
>    {
>    char *name, *address;
>    long id;
>    }

> int main()
>    {
>    Person *Bob[2];
>    int i;

>    for (i = 0 ; i < 2 ; i++)
>       Bob[i] = malloc(sizeof(Person));

>    strcpy(Bob[1].name, "Bob Smith");
>    strcpy(Bob[1].address, "1234 Main St.");
>    Bob[1].id = 100;
>    strcpy(Bob[2].name, "Bob Jones");
>    strcpy(Bob[2].address, "1000 5th St.");
>    Bob[2].id = 101;

>    for (i = 0 ; i < 2 ; i++)
>       printf("ID of Bob %d is %lf", i, Bob[i].id)
>    return(0);
>    }

--




Sat, 10 Nov 2001 03:00:00 GMT  
 Extremely short -extremely complicated
You know what... I kinda got the wierdest feeling looking through the
code, especially the first program, that this was a "What's wrong with
this code" assignment from a class somewhere. I'm probably wrong, so
even though I would never ever dream of solving a problem for someone's
homework, I'll assume that I'm wrong.

Here's a rundown of your first program then.

Basically there were a whole bunch of problems, and some of them a lot
more subtle than other replies to the original message actually noticed.

First up, that 's' array declaration. Your code implies that you want to
be dealing with 10 strings, so you really need 10 to be the first
element in the array. The second element then is the maximum size of
each string. Now, your code later tries to dump in 'Copying into string
' followed by a number, so you're second array element needs to be big
enough to hold that many characters. For the sake of argument, and given
that memory on this scale is a non-issue in modern computers, make the
second element something safe, like 100.

char s[10][100];

That then sets you up with an array of 10 strings, each of which can
hold at most 100 characters (including the \0 terminator that any string
in C needs to have).

Your next line starts a loop to actually put values into those strings.
The problem though is that in Ansi C you need to declare the integer
that you are using to count in the For loop before you actually hit the
loop. For example you did this

for (i=0; i<10 ; i++);

when you should have done this

int i;
for (i=0; i<10; i++)

Now we hit the first subtle problem. Your original For line had a semi
colon at the end, which means that the line that follows the for would
not actually be included in your for loop. Remove the semi colon from
the end of the line, as I have done above.

The next chunk of code you had using strcpy to put the text 'String
contains ', followed by a number, into the string indexed by the i
variable. Well, strcpy won't do that. All strcpy does is copy one
string, character by character into the first string.

What you need is the sprintf function, as many people pointed out. You
use sprintf the same way as printf; pass it a string with % items in as
placeholders for further data, andthen pass that further data in as a
parameter to. The difference between sprintf and printf though is that
you also need to specify a place to store the final string in; sprintf
builds strings and stores them, printf just prints them out. So, the
line you need is

sprintf( s[i], "String contains %d", i);

At runtime %d gets replaced with the value of i (translated into Ascii
text of course) and then the whole string is copied into string array i.
Since you didn't specify the second set of indexes on that string array,
and only said s[i], the compiler assumes that you are passing across the
address of an array of characters, and goes ahead and dumps the new
string into that array.

The next for line you had was fine.

Finally, you want to print out your 10 strings from the array. You had
couple of subtle nasties on this line too.  You said

printf("\nString %d contains %c", s[i]);

Well, first up, you need an integer to go with that %d, so your printf
line should have ended
..", i, s[i] );

Second that %c modifier makes printf look for a single character. You
are passing across an entire string, so  you need to replace %c with %s.

So, your final print line looks like this

printf("\nString %d contains %s", i, s[i] );

And then it should all work just fine.

Hope that helps. Haven't taken a peek at your second program yet - bear
with me.

Pete.



Sun, 11 Nov 2001 03:00:00 GMT  
 Extremely short -extremely complicated

Quote:

>Here are small programs that I'm trying to find errors in following
>programms. I already took me hours of debugging and it seems I'm getting
>nowhere. Please help.
>/*program #1*/
>int main()
>   {
>   char s[5][10];

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

As you have defined s, you have 5 arrays of 10 characters, so it should be
i < 5.

Quote:
>      strcpy(s[i], "Copying into string %d", i);

Bounds error - strlen("Copying into string 1") > 9, so a likely core dump is
coming your way.

Quote:
>   for (i = 0 ; i < 10 ; i++)

See above.

Quote:
>      printf("\nString %d contains %c", s[i]);

s[i] contains a char *, %c expects an argument of type char.  replace %c
with %s to do something approximating a fix.

Quote:
>/*program #2*/
>typedef struct Person
>   {
>   char *name, *address;
>   long id;
>   }

Is it just me, or does this need a semicolon?

After a good start with this program, you fell into the trap I'm seeing with
my classmates:

Quote:
>   strcpy(Bob[1].name, "Bob Smith");
>   strcpy(Bob[1].address, "1234 Main St.");
>   Bob[1].id = 100;
>   strcpy(Bob[2].name, "Bob Jones");
>   strcpy(Bob[2].address, "1000 5th St.");
>   Bob[2].id = 101;

The range of a C array defined as array[n], is [0, n - 1] so your subscripts
in this case should be 0 and 1 - not 1 and 2.

--
-----------------------------------------------------------------------
#include <disclaimer.h>   | GE/CS d- s+:+ a--- C++(+++)>$ UL+++ P+ L++ !E
Matthew Palmer          | W-- N++ o? K- w--(---) O- M-(--) V? PS+ PE Y+



Sun, 11 Nov 2001 03:00:00 GMT  
 Extremely short -extremely complicated
I hope that StarOffice is written by other authors.
Quote:

> Hello,

> Am 25.05.99, 11:28:31, schrieb Hendrik Kuehne

> -extremely complicated:

> > > /*program #1*/
> > > int main()
> > >    {
> > >    char s[5][10];

> > >    for (i = 0 ; i < 10 ; i++);
> > >       strcpy(s[i], "Copying into string %d", i);

> > Here you have to use the second index not the first one to set
> > characters in the strings.
> > The loop is as follows:
> >       for( i=0 ;i < 5 ;i++ ) {        /* to access each string */
> >               for( j=0 ;j < 10 ;j++ ) /* to access the characters in
> the current
> > string */
> >                       strcpy(s[i][j], "Copying into string %d", i);
> >       }
> > The variable i selects string 1 to 5 and variable j selects the
> > characters 1 to 10 in string no. i.

> And additionally the argument of strcpy is way too long (more than 9
> chars) for the array.

> Greetings,
>   Thomas



Sun, 11 Nov 2001 03:00:00 GMT  
 Extremely short -extremely complicated

Quote:

> Here are small programs that I'm trying to find errors in following

Us in Yugoslavia are getting bombed all day long but still didn't go
this
nuts! :)

Quote:
> /*program #1*/

Now, where's your #include?!
Quote:
> int main()
>    {
>    char s[5][10];

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

                               ^
A semi colon marks the end of the loop! This means that you've written
an
EMPTY LOOP and the statement that follows (which is wrong on its own
behalf)
executes only once, and, oh the irony, with the value i of 10, which is
out
of bounds for s, one way or the other, even if you did not mix up the
indices.

Quote:
>       strcpy(s[i], "Copying into string %d", i);

strcpy does not work this way. It has two arguments, dest and source.
One
way or the other, string "Copying into..." is FAR LONGER than s could
possibly contain. Try s[5][100]

Quote:
>    for (i = 0 ; i < 10 ; i++)
>       printf("\nString %d contains %c", s[i]);
>    return(0);
>    }

This is also wrong since you have two space-fillers (%d and %i) and only
one variable to print. But, no worries, since the above code is all a
blunder and this would not work as expected even if it had been written
right.

Quote:
> /*program #2*/
> typedef struct Person
>    {
>    char *name, *address;
>    long id;
>    }

Good ol' Bob has got *name, and *address, but no malloc to allocate
memory
for his name. Try something line name[100], address[100] or try
remembering
what malloc is for.

Quote:
>    for (i = 0 ; i < 2 ; i++)
>       printf("ID of Bob %d is %lf", i, Bob[i].id)
>    return(0);

Now, lines like these could be useful in program #1, could they?


Mon, 12 Nov 2001 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Extremely Annoying Bug in the VS IDE

2. (Extremely fruistrated) CFormView

3. HELP:Extremely Simple problem -- Thanks!!

4. @@@@@@ EXTREMELY URGENT / HELP NEEDED!!!

5. Extremely bizarre malloc problem

6. need help in extremely simple c program

7. extremely large datatypes (10^31 and greater)

8. Extremely Simple ?

9. Extremely Boring discussion of calendar

10. my file size is strangley extremely large....

11. Extremely simple question

12. VC 6.0 editor extremely slow!

 

 
Powered by phpBB® Forum Software