Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 23 Apr 2001 10:34:27 -0700
From:      "Jeremiah Gowdy" <jgowdy@home.com>
To:        <david@angra.uac.pt>, <questions@FreeBSD.ORG>
Subject:   Re: fgets/fputs
Message-ID:  <000701c0cc1b$a7c458a0$015778d8@sherline.net>
References:  <0104231717100C.05409@david>

next in thread | previous in thread | raw e-mail | index | archive | help

----- Original Message -----
From: "David" <david@angra.uac.pt>
To: <questions@FreeBSD.ORG>
Sent: Monday, April 23, 2001 10:08 AM
Subject: fgets/fputs


> I have a doubt on libc:
>
> Both fputs and fgets functions use a char* instead of char[255]
> fgets (char *s, int count, FILE *stream)
> fputs (const char *s, FILE *stream)

Dude, why would they use char[255] ?  They're any size you allocate.

void main(void)
{
char str[255];
fgets(str,255,stdin);
fputs(str,stdout);
}

or

void main(void)
{
char *str;
str=malloc(255);
fgets(str,255,stdin);
fputs(str,stdout);
}

> but the program only works when I define char[255] instead of char* as I
show in
> the source code below:...Why?

char[255] allocates 255 bytes of data.  char* is only a pointer, which
points to nothing until you allocate some memory for it to point to.  Your
question is not really about libc.  It's about a lack of basic understanding
of "strings" (or lack therof) and pointers in C.  You need a C programming
book.  The questions you're asking would be the first 3 chapters of any good
C book.  Good luck :)



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?000701c0cc1b$a7c458a0$015778d8>