Re: some unanswered questions on C

From: Rich Teer (rich.teer_at_rite-group.com)
Date: 06/19/03


Date: Thu, 19 Jun 2003 21:50:09 GMT

On 19 Jun 2003, seemanta dutta wrote:

> greetings linux gurus...

No Linux here, just UNIX...

> thanks for ur patience in pointing out mistakes of beginners...

No worries, we've all gotta learn somehow.

> i have a few more questions to ask:
>
> 1. i feel ashamed, but what exactly is an uninitialised pointer?
> is
> int *localVar;
> *localVar = 3 ;
> an uninitialised pointer?

Yes. You've said that localvar is a pointer to an int,
but you haven't initialised it to point to anything.
In the second line you try to store something in the int
to which localvar points to, but you have no idea where
it points.

> or is this is an *initialised* pointer?
> int *localVar;
> localVar = malloc(sizeof int);

Yes. malloc returns the address of the space it allocated,
which is assigned to localvar. Consequently, localvar now
points to somewhere meaningful.

> 2. in some of my programs i have to read from a file and write to a
> different file using fgets and fprintf...
>
> what i want to ask is that when i declare my buffer for fgets as
> char *buff;

Again, you've said that buff is a pointer to a character,
but you haven't declared any space for the buffer.

> my program compiles alright but while running it gives a seg fault..i
> can understand this is because of a buffer overflow, but if i use a
> declaration like this for my buffer
> char buff[80];
> my seg fault disappears...

Yes, becauase now you've allocated 80 bytes to buff.

> but here i am implicitly assuming my data will never be larger that 80
> bytes...if my data exceeds 80 bytes there occurs another seg fault and
> my program exits...
> is it a good practice to use limited size buffers for file i/o?

Yes, but you obviously have to account for the situation
where you have to go through several read and write iterations.

> it is there that i am facing the problem with limited size buffers for
> performing file i/o with 80 byte-sixed buffers...

You have two options:

        1. Just consume the line in 80 character chunks

        2. Write your own version of fgets that will dynamically
           allocate enough memory for the line as it's being read in.

> 3. in some of my programs like this..
>
> int main()
> {
>
> char deststr[20],srcstr1[20]="foo",srcstr2[20]="bar";
> deststr=strcat(srcstr1,srcstr2); // insertion of 4 given
> lines made here in place of this line...
> puts(deststr);
> return 0;
> }
>
> but the above program compiles with the following error: t.c: In
> function `main':
> t.c:8: incompatible types in assignment
> if the man page says that strcat returns a 'char *' and since the name
> of a char array implies a pointer to a char, why is this giving an
> error?

In this case, you could probably just ignore strcat's return value.

> what to do? it is a catch22 kind of situation for me... i cannot use
> *deststr because of buffer overflows,neither can i use deststr[20] for
> it does allow me to use strcat as i want to...
> and gives an error

No disrespect, buy I strongly recommend you get, and read, a good
C programming book. There's a lot of fundamentals that you don't
seem to have got yet, and would take a very long post to explain.

> is that an unacceptable practice to return address of an auto
> variable?

Yes.

Hope this helps somewhat,

-- 
Rich Teer, SCNA, SCSA
President,
Rite Online Inc.
Voice: +1 (250) 979-1638
URL: http://www.rite-online.net


Relevant Pages

  • Re: Write to file & Change Directory
    ... working, of course, but the do_*functions operate in context of the ... So, before initializing, I have to declare what type of pointer it will ... as well as a buffer in his own address space. ...
    (comp.os.minix)
  • Re: unknown software exception with SampleGrabber
    ... > to declare the class global. ... > How do I get a pointer to the buffer, where I can work on the buffer? ... > I have read something about in the SampleCB function, ...
    (microsoft.public.win32.programmer.directx.video)
  • Re: unknown software exception with SampleGrabber
    ... I created the class c_GrabberCB inside the function AddGrabber, ... to declare the class global. ... How do I get a pointer to the buffer, where I can work on the buffer? ...
    (microsoft.public.win32.programmer.directx.video)
  • [net-next PATCH 4/5] igb: Add support for enabling VFs to PF driver.
    ... pointer to the hardware struct ... under the terms and conditions of the GNU General Public License, ... * @msg: The message buffer ... * returns SUCCESS if it successfully copied message into the buffer ...
    (Linux-Kernel)
  • Re: some unanswered questions on C
    ... A pointer variable that's never been given a value. ... you don't know what memory you're modifying. ... >what i want to ask is that when i declare my buffer for fgets as ... "char *buffer" creates a pointer, ...
    (comp.unix.programmer)