Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!ames!amdahl!dgcad!dg-rtp!gamecock!hagins From: hagins@gamecock.rtp.dg.com (Jody Hagins) Newsgroups: comp.lang.c Subject: Re: pointer problems, help! Message-ID: <1991Mar22.193636.10853@dg-rtp.dg.com> Date: 22 Mar 91 19:36:36 GMT References: <1991Mar22.082225.24948@bronze.ucs.indiana.edu> Sender: usenet@dg-rtp.dg.com (Usenet Administration) Reply-To: hagins@gamecock.rtp.dg.com (Jody Hagins) Distribution: na Organization: Data General Corporation, Research Triangle Park, NC Lines: 124 In article <1991Mar22.082225.24948@bronze.ucs.indiana.edu>, mitchemt@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 . |> 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@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.