[comp.sys.apollo] String copy problem

timv@IMAX.ENG.UIOWA.EDU (Tim VanFosson) (08/18/88)

   >> I don't understand why following codes don't work:
   >> #include <stdio.h>
   >> #include <strings.h>
   >> main(argc, argv)
   >> int      argc;
   >> char    *argv;
   >> {
   >> char    *str1;
   >> strcpy(str1, argv[1]);
   >> }
   >
   >This is not a problem with the apollo, but a problem with your understanding
   >of pointer syntax. Relax, it's a common mistake! The definition
   >
   >     char *str1:
   >
   >means "str1" is a POINTER to an array of characters. In other words, str1
   >is supposed to contain the address of a character array. But in this code
   >fragment, you have not assigned an address to "str1", so the variable contains
   >a "garbage" address. Therefore, when the program tries to run, it tries to copy 
   >the string pointed to by argv[1] to a non-existant character array. This is
   >why you get an "access violation". The way to fix this would be to define
   >str1 like this:
   >
   >    char str1[255]; 
   >(or replace the 255 with some number more appropriate to your application)
   >so that there is an array defined for strcpy to copy into. 

      You could also use malloc to allocate space for the string, i.e.,

           str1 = (char *)malloc( sizeof( *argv[1] ));

   >
   >I would suggest that you get a good book on basic C programming before you 
   >go any further. I don't know of any offhand, but there *are* some good books
   >out there that would help you avoid the more common C mistakes.
   >

         You might try _The C Programming Language_ by Kernighan and
      Ritchie, published by Prentice-Hall.  It is considered to be
      *the* C reference (one company shipped it to us as documentation :-) ).
---
Timothy VanFosson                           Internet : timv@imax.eng.uiowa.edu
Systems Analyst                             US Mail  : CAD-Research
University of Iowa                                     1405 Engineering Building
Phone : (319) 335 - 5728                               Iowa City, Iowa 52242

rees@CITI.UMICH.EDU (08/19/88)

          You could also use malloc to allocate space for the string, i.e.,
     
               str1 = (char *)malloc( sizeof( *argv[1] ));

Well that's clearly wrong, but I want to point out that a lot of programs
try to do this:

               str1 = (char *) malloc(strlen(argv[1]));

which is also wrong.  It happens to work on some systems because of a
coincidence in the way malloc works.  The correct code leaves room for
the null terminator:

               str1 = (char *) malloc(strlen(argv[1]) + 1);

If you don't do this, the null terminator wipes out whatever is after the
allocated space.  In some implementations, this is OK, because the next
thing will be another malloc block with an address in the first word.
As long as the address is less than 2^24, the first byte will be null
(on big-endian machines) and you're OK.  On an Apollo it's not OK.
-------

dave@jplopto.uucp (Dave Hayes) (08/19/88)

>         You might try _The C Programming Language_ by Kernighan and
>      Ritchie, published by Prentice-Hall.  It is considered to be
>      *the* C reference (one company shipped it to us as documentation :-) ).

I was trying to steer him *away* from that book! That book is good as a reference,
but *not* as a tutorial!! DO you know of any other tutorial-like books that teach
C in a more complete and rigourous fashion?

------===<<<Dave Hayes>>>===------
dave%jplopto@jpl-mil.jpl.nasa.gov
{cit-vax,ames}!elroy!jplopto!dave   

Jinfu@cup.portal.com (08/21/88)

>>         You might try _The C Programming Language_ by Kernighan and
>>      Ritchie, published by Prentice-Hall.  It is considered to be
>>      *the* C reference (one company shipped it to us as
>>      documentation :-) ). 
>
>I was trying to steer him *away* from that book! That book is good as
>a reference, but *not* as a tutorial!! DO you know of any other
>tutorial-like books that tea> C in a more complete and rigourous
>fashion? 


Yes, I have the book but I'd rather read an Apollo manual :-)

The book I use to learn C is 'The C Primer, 2nd Edition' by Les
Hancock and Morris Krieger. It's just good enough to show me what C is
but lack of antidebugging/debugging (to borrow the terms from 'Oh
Pascal') information like this.

Thanks to all the people who sent me mails and posted messages about
my mistake.

Jinfu Chen

chinn@apciseaapcisea.UUCP (3DE535B7.20014935) (08/23/88)

In article <8560@elroy.Jpl.Nasa.Gov>, dave@jplopto.uucp (Dave Hayes) writes:
> 
> >         You might try _The C Programming Language_ by Kernighan and
> >      Ritchie, published by Prentice-Hall.  It is considered to be
> 
> I was trying to steer him *away* from that book!...
> ...DO you know of any other tutorial-like books that teach
> C in a more complete and rigourous fashion?

This is probably not the correct news group for this; but since you asked...

I particularly like two books:
	"Advanced C: Food for the Educated Palate"
		Narain Gehani, 1985 Computer Science Press

	"C Wizards's Programming Reference"
		W. David Schwaderer, 1985 Wiley Press

...uw-beaver                                    david m. chinn
      !apcisea                                  apollo computer inc
           !chinn                               bellevue sales office
   (206) 453-5544                               bellevue, washington