pointer problems, help!
Jody Hagins
hagins at gamecock.rtp.dg.com
Sat Mar 23 05:36:36 AEST 1991
In article <1991Mar22.082225.24948 at bronze.ucs.indiana.edu>, mitchemt at silver.ucs.indiana.edu (Terry Mitchem) writes:
|>
|> I am having some major problems getting a piece of code to work.
|> It seems that everytime I change the contents of one string, it affects
|> another one. For example, when I build the filename below, it gets wiped
|> out when I null the members of "target_player". The code is below, and below
|> that is the datafile I am using.
|>
|> void edit_category()
|> {
|> struct
|> {
|> char *card_number;
|> char *quantity;
|> char *first_name;
|> char *last_name;
|> char *price_mint;
|> char *price_ex;
|> } target_player;
|>
|> char filename[80],carriage_return[5];
^^^^^^^^^^^^^^^^^
This means that you are setting aside enough space for 80 chars, and
that memory has a starting address. The starting address is stored in
the variable <filename>.
|> int infile,bytes;
|>
|> *filename=NULL;
|>
|> strcat(filename,".\\"); strcat(filename,category.brand);
|> strcat(filename,"\\"); strcat(filename,category.type);
|> strcat(filename,"\\"); strcat(filename,category.year);
|> strcat(filename,"\\"); strcat(filename,category.other);
|> strcat(filename,"\\data");
Ever heard of sprintf()?
|>
|> *target_player.card_number=NULL; *target_player.quantity=NULL;
|> *target_player.first_name=NULL; *target_player.last_name=NULL;
|> *target_player.price_mint=NULL; *target_player.price_ex=NULL;
Here you are initializing a piece of memory to NULL.
Yeh, I know, I'm setting the first character to null, just like filename
above. (I think you think that's what you are doing)
Well, no. See, target_player.card_number (as well as the rest of the members
of this structure) is a char pointer. That means it POINTS TO A CHAR. It
takes up enough space in memory to store the address of a char data type.
However, it's value is not initialized, and therefore could be pointing out
in space to anywhere. When you say *target_player.card_number = NULL, you are
saying "set the char that target_player.card_number points to, to NULL. However,
target_player.card_number doesn't point to anything that we know of. Therefore
you are setting some unknown place in memory (probably 0, but that's another
story) to NULL.
|>
|> infile=open(filename,O_RDONLY);
|> if (infile==-1) exit(1);
|>
|> read(infile,target_player.card_number,5);
|> read(infile,target_player.quantity,3);
|> read(infile,target_player.first_name,21);
|> read(infile,target_player.last_name,21);
|> read(infile,target_player.price_mint,7);
|> read(infile,target_player.price_ex,7);
Again, you obviously do not understand the concept of pointers. I suggest
that you read through the section on pointers in a good C ref. book.
But hey, Jody, what are you talking about? read() is supposed to read a
certain number of bytes into the char * passed as the 2nd arg.
Yeh, you got that right!
Huh?
Well, think of it this way. read() reads a certain number of bytes, and
puts the result into the buffer pointed to by the 2nd arg. However, you
have not allocated space for a buffer. What's more, you have uninitialized
pointers, so you are reading into "unknown" memory areas (again, probably 0).
Remember that allocating a pointer is not the same as allocating the memory
that the pointer points to! You need to have some space in which to read the
data.
|> }
|> ----------------------------------------------------------------------------
|> Here is the datafile:
|>
|> 8 2 joe montana .75 .40
|> 9 2 christian okoye .20 .10
|>
|> Any and all help is appreciated. I don't seem to be able to get
|> anything useful out of K&R to help me. I am compiling with turbo-c.
|> Thanks in advance
|> Terry
|>
Seriously, this is not a flame. However, I do think you need to go back to
your text, and instructor, and get a grasp on the difference between a pointer
and the data the pointer is referencing.
--
Jody Hagins
hagins at gamecock.rtp.dg.com
Data General Corp.
62 Alexander Dr.
RTP, N.C. 27709
(919) 248-6035
Nothing I ever say reflects the opinions of DGC.
More information about the Comp.lang.c
mailing list