473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C file

#include <stdio.h>

main(){
char *cap;
printf("Enter code-");
fflush(stdout);
fgets(cap,100,stdin);
printf(cap);}

Now this seems to work much better. Decalring a pointer to a char seems to
be the default of fgets first parameter. This compile without even a
complaint by the compile for a cast. So if someone entered something that
was 200 chars long. There would be an error but not a buffer overrun?

Bill
Nov 20 '07 #1
21 1424
Bill Cunningham wrote:
#include <stdio.h>

main(){
char *cap;
printf("Enter code-");
fflush(stdout);
fgets(cap,100,stdin);
Where does cap point?

--
Ian Collins.
Nov 20 '07 #2

"Ian Collins" <ia******@hotmail.comwrote in message
news:5q************@mid.individual.net...
Bill Cunningham wrote:
>#include <stdio.h>

main(){
char *cap;
printf("Enter code-");
fflush(stdout);
fgets(cap,100,stdin);

Where does cap point?

--
Ian Collins.
To a type char in this file.

Bill
Nov 20 '07 #3
Bill Cunningham wrote:
"Ian Collins" <ia******@hotmail.comwrote in message
news:5q************@mid.individual.net...
>Bill Cunningham wrote:
>>#include <stdio.h>

main(){
char *cap;
printf("Enter code-");
fflush(stdout);
fgets(cap,100,stdin);
Where does cap point?
*Please* don't quote signatures.
>
To a type char in this file.
No, it is uninitialised.

--
Ian Collins.
Nov 20 '07 #4
No, it is uninitialised.
>
So then it is declared but not assigned. Would assigning it involve the
ampersand?

Bill
Nov 20 '07 #5
Bill Cunningham wrote:
>No, it is uninitialised.
So then it is declared but not assigned. Would assigning it involve the
ampersand?
That depends what you assign it to. Do you have a C book?

--
Ian Collins.
Nov 20 '07 #6
In article <ztp0j.7359$oL5.1244@trnddc05>, Bill Cunningham
<no****@nspam.comwrote on Tuesday 20 Nov 2007 5:31 am:
#include <stdio.h>

main(){
int main(void) { ...

is better form for the current Standard.
char *cap;
printf("Enter code-");
fflush(stdout);
fgets(cap,100,stdin);
Where have you allocated memory for 100 char objects and set 'cap' to
point to their beginning?

This fgets() call invokes undefined behaviour because it deferences an
uninitialised pointer and attempts to write to memory it does not own,
or should not write to.
printf(cap);}
This is horrible code style. What do you gain by placing the closing
brace of main() on the same line as the last of it's statements? Also
include a return statement.
Now this seems to work much better.
If this works, it's purely by chance. It's horribly broken.
Decalring a pointer to a char
seems to be the default of fgets first parameter.
Yes. But in almost all situations a pointer alone is useless without it
pointing to some region of valid storage.
This compile without
even a complaint by the compile for a cast.
C assumes you know what you are doing.
So if someone entered
something that was 200 chars long. There would be an error but not a
buffer overrun?
It wouldn't be a "buffer overrun" because you have used fgets() instead
of the deprecated gets(), but that's irrelevant to the code you have
shown above.

Whether you read one char or a million char, you are still not
allocating any storage for them. You are merely supplying an
indeterminate pointer value to fgets() and invoking undefined
behaviour. Two possible solutions include:

char input[100];
fgets(input, sizeof input, stdin);

or

char *cap;
size_t buffer_size = 100;
cap = malloc(buffer_size);
if (!cap) { /* no memory */ }
fgets(cap, buffer_size, stdin);
/* proceed */

Nov 20 '07 #7
santosh wrote:
In article <ztp0j.7359$oL5.1244@trnddc05>, Bill Cunningham
<no****@nspam.comwrote on Tuesday 20 Nov 2007 5:31 am:
>#include <stdio.h>

main(){

int main(void) { ...

is better form for the current Standard.
> char *cap;
printf("Enter code-");
fflush(stdout);
fgets(cap,100,stdin);
...
>So if someone entered
something that was 200 chars long. There would be an error but not a
buffer overrun?

It wouldn't be a "buffer overrun" because you have used fgets() instead
of the deprecated gets(), but that's irrelevant to the code you have
shown above.
It depends on the definition of buffer overrun:
1. It isn't a buffer overrun because there is no buffer!
2. It is an overrun because characters are placed in memory not
allocated for that purpose.

--
Thad
Nov 20 '07 #8
Bill Cunningham wrote:

Get a good C book - Kernighan and Ritchie, The C Programming Language,
2nd Edition, is old but well worth the money.
#include <stdio.h>
Well done for including this.
>
main(){
This should be int main(void)
char *cap;
A 'char *' is a pointer-to-char. It can also point to the first char in
a char array - and so, using sloppier language, point to an
array-of-char. But here it doesn't point to anything.

A 'char *' is a way of accessing chars, but in this program you have no
chars to access.
printf("Enter code-");
fflush(stdout);
fgets(cap,100,stdin);
Well done on not using gets().

But now the problem becomes apparent. fgets() does not provide the chars
you need to write to. You provide a 'char *' to fgets() which points to
the chars which /you/ have to provide. Here you haven't provided
anything - so fgets() has no allocated memory to write to.
printf(cap);}
This should be:
printf(cap);
return 0;
}

Now, a question which you should be asking is: "If fgets() is writing to
chars which don't exist or haven't been allocated, why did my program work?"

The answer is that there exist certain programs which break the rules of
C, and when that happens the C standard places no restriction on the
program behaviour. This is known as "Undefined behaviour". What that
means is your program may work perfectly - or it may output subtly wrong
answers, or crash, or slowly eat up all your memory, or anything it
likes. If you compile the same C program on a different compiler you may
find it doesn't work. It may even crash on a new version of the same
compiler!

This means that learning C by trial-and-error alone is a Bad Idea; just
because a program compiles and runs as expected, doesn't mean that it is
valid C.

This is why the first sentence in this post was "Get a good C book".
Nov 20 '07 #9
On 20 Nov, 00:01, "Bill Cunningham" <nos...@nspam.comwrote:
#include <stdio.h>

main(){
char *cap;
printf("Enter code-");
fflush(stdout);
fgets(cap,100,stdin);
printf(cap);}

Now this seems to work much better.
better than what?
Decalring a pointer to a char seems to
be the default of fgets first parameter.
it's not "the default", it's the only possible answer

char *fgets(char*, int, FILE*);

the only confusion is when a array "decays" to a pointer

char buffer[200];

fgets(buffer, 200, stdin);

a pointer (char*) is passed as the first parameter

<snip>

--
Nick Keighley
Nov 20 '07 #10
Two possible solutions include:
>
char input[100];
fgets(input, sizeof input, stdin);
This looks good.
or

char *cap;
size_t buffer_size = 100;
cap = malloc(buffer_size);
if (!cap) { /* no memory */ }
fgets(cap, buffer_size, stdin);
/* proceed */
This is totally incomprehensible to me. I recognize all the statements
but I don't know what they are saying because of my limited C knowledge.

Bill
Nov 20 '07 #11

"Ian Collins" <ia******@hotmail.comwrote in message
news:5q************@mid.individual.net...
Bill Cunningham wrote:
>>No, it is uninitialised.
So then it is declared but not assigned. Would assigning it involve
the
ampersand?
That depends what you assign it to. Do you have a C book?
I have k&r2 which everyone brags about and I guess I can just sit down
and take it one step at a time and do the excercises. I also have "C
Unleased" a very large book. It might be best to use k&r2.

Bill
Nov 20 '07 #12
Bill Cunningham wrote:
I have k&r2 which everyone brags about
"brags"? "BRAGS"???? We recommend it, because it's good. I haven't
noticed anyone bragging about it.
and I guess I can just sit down
and take it one step at a time and do the excercises.
That may be more productive that to keep guessing and posting to
comp.lang.c - we're willing to help when you get stuck, but a newsgroup
is a poor medium for basic tuition, in my opinion.
Nov 20 '07 #13
"Bill Cunningham" <no****@nspam.comschrieb im Newsbeitrag
news:gQC0j.24080$K_3.12413@trnddc03...
Two possible solutions include:
>>
char input[100];
fgets(input, sizeof input, stdin);

This looks good.
>or

char *cap;
size_t buffer_size = 100;
cap = malloc(buffer_size);
Ask the system to give you buffer_size bytes and assing cap the address of
the first of these
> if (!cap) { /* no memory */ }
if cap is 0 or actually NULL, malloc() didn't get you the memory you asked
it for, so you better stop processing right here
> fgets(cap, buffer_size, stdin);
pass the address of the first byte malloc() allocated for you and the size
of that buffer to fget() for it to store the input it reads from stdin
> /* proceed */

This is totally incomprehensible to me. I recognize all the statements
but I don't know what they are saying because of my limited C knowledge.
Bye, Jojo
Nov 20 '07 #14
In article <1SC0j.24121$K_3.4340@trnddc03>, Bill Cunningham
<no****@nspam.comwrote on Tuesday 20 Nov 2007 8:45 pm:
>
"Ian Collins" <ia******@hotmail.comwrote in message
news:5q************@mid.individual.net...
>Bill Cunningham wrote:
>>>No, it is uninitialised.

So then it is declared but not assigned. Would assigning it
involve
the
ampersand?
That depends what you assign it to. Do you have a C book?
I have k&r2 which everyone brags about and I guess I can just sit
down and take it one step at a time and do the excercises.
If K&R2' learning curve seems a bit steep to you (it expects you to have
some rudimentary programming knowledge) try one of the tutorials listed
below. Steve Summit's version is a bit more "gentle" than Tom Torf's.

<http://www.eskimo.com/~scs/cclass/>
<http://cprog.tomsweb.net/>
I also have "C Unleased" a very large book. It might be best to use
k&r2.
_C Unleashed_ will certainly make a lot more sense _after_ K&R.

Nov 20 '07 #15
[comp.lang.c] Bill Cunningham <no****@nspam.comwrote:
#include <stdio.h>
main(){
char *cap;
printf("Enter code-");
fflush(stdout);
fgets(cap,100,stdin);
printf(cap);}
Aside from all the other egregious failures of this code, the line

printf(cap);

can lead to tragedy all by itself. If cap happens to contain a format
specifier such as %s, undefined behavior results. puts() avoids this
potential pitfall, or printf("%s",cap) if you insist on printf().

--
C. Benson Manica | I appreciate all corrections, polite or otherwise.
cbmanica(at)gmail.com |
----------------------| I do not currently read any posts posted through
sdf.lonestar.org | Google groups, due to rampant unchecked spam.
Nov 20 '07 #16
Ian Collins wrote:
Bill Cunningham wrote:
>>No, it is uninitialised.
So then it is declared but not assigned. Would assigning it involve the
ampersand?
That depends what you assign it to. Do you have a C book?
Bill has been posting this kind of garbage code for at least 18 months.
I recall several people pointing out that C programming doesn't seem to
be for him, but he keeps coming back...
Nov 20 '07 #17
Mark McIntyre said:
Ian Collins wrote:
>Bill Cunningham wrote:
>>>No, it is uninitialised.

So then it is declared but not assigned. Would assigning it involve
the
ampersand?
That depends what you assign it to. Do you have a C book?
Bill has been posting this kind of garbage code for at least 18 months.
At least five years, actually. The first article he posted to this group
that I can find is dated 5 June 2002, in which he was recommending K&R2
(although he wasn't sure if the book was still in print, as he hadn't
spoken to Dennis Ritchie for a while).

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 20 '07 #18
Richard Heathfield wrote:
Mark McIntyre said:
>Bill has been posting this kind of garbage code for at least 18 months.

At least five years, actually. The first article he posted to this group
that I can find is dated 5 June 2002, in which he was recommending K&R2
(although he wasn't sure if the book was still in print, as he hadn't
spoken to Dennis Ritchie for a while).
A: Why haven't you talked to Dennis for a while?
B: Say, would you talk to a person, that has not
the faintest grasp of C?
A: No, of course not!
B: See, and nor does Dennis.

:)
Nov 20 '07 #19
Bill has been posting this kind of garbage code for at least 18 months.
I recall several people pointing out that C programming doesn't seem to be
for him, but he keeps coming back...
More than 18 months Mark have I posted. And C seems to be for everyone.
I've looked at other languages too. The only one I can really pound out code
with is Basic older than C. I will try to learn more (about C)but I don't
think I am going to just give up on C.

Bill
Nov 20 '07 #20
If K&R2' learning curve seems a bit steep to you (it expects you to have
some rudimentary programming knowledge) try one of the tutorials listed
below. Steve Summit's version is a bit more "gentle" than Tom Torf's.

<http://www.eskimo.com/~scs/cclass/>
<http://cprog.tomsweb.net/>
>I also have "C Unleased" a very large book. It might be best to use
k&r2.
Oh thanks Santosh yes this looks like a good tutorial from k&r2's pages.

Bill
Nov 21 '07 #21
Bill Cunningham wrote:
Two possible solutions include:
> char input[100];
fgets(input, sizeof input, stdin);

This looks good.
>or

char *cap;
size_t buffer_size = 100;
cap = malloc(buffer_size);
if (!cap) { /* no memory */ }
fgets(cap, buffer_size, stdin);
/* proceed */

This is totally incomprehensible to me. I recognize all the statements
but I don't know what they are saying because of my limited C knowledge.

Bill

Bill,

Perhaps you could explain some of your personal challenges: Memory,
learning and stuff. Using C as therapy.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 22 '07 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give...
5
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. ...
7
by: Joseph | last post by:
Hi, I'm having bit of questions on recursive pointer. I have following code that supports upto 8K files but when i do a file like 12K i get a segment fault. I Know it is in this line of code. ...
3
by: StGo | last post by:
How can i read/write file's custom attributs(like subject,author...) in C#??? Thanks :))
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
1
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved...
3
by: Shapper | last post by:
Hello, I created a script to upload a file. To determine the file type I am using userPostedFile.ContentType. For example, for a png image I get "image/png". My questions are: 1. Where can...
0
by: troutbum | last post by:
I am experiencing problems when one user has a document open through a share pointing to the web site. I use the dsolefile to read the contents of a particular directory and then display them in a...
0
by: thjwong | last post by:
I'm using WinXP with Microsoft Visual C++ .NET 69462-006-3405781-18776, Microsoft Development Environment 2003 Version 7.1.3088, Microsoft .NET Framework 1.1 Version 1.1.4322 SP1 Most developers...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.