473,383 Members | 1,874 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,383 software developers and data experts.

pointer concepts

Hi,

I'm a newbie. I need to clear my pointer concepts.

the code below gives segmentation fault when i use char* str;
int main()
{
char *str;
printf("Enter a string to reverse\n");
scanf("%s",&str);
strlen(str);
}

if I use char str[20] it runs fine.
int main()
{
char str[20];
printf("Enter a string to reverse\n");
scanf("%s",&str);
strlen(str);
}

Can you please explain why?

Thanks

Feb 10 '06 #1
31 3695
Newbie wrote:
Hi,

I'm a newbie. I need to clear my pointer concepts.

the code below gives segmentation fault when i use char* str;
int main() use: int main(void) {
char *str;
printf("Enter a string to reverse\n");
scanf("%s",&str); Here's the problem. str points to somewhere in memory
but you have no idea where and whether or not that area
belongs to you. You need to allocate memory (dynamically).
You can run into the problem again if you read more
characters than you allocated. strlen(str); return something. }

if I use char str[20] it runs fine. Well, yes, because you str[20] is automatically
allocated and you tell it exactly how much memory
you want. To dynamically allocate memory you need
to use a pointer and allocate memory for it (malloc & co). int main() int main(void) {
char str[20];
printf("Enter a string to reverse\n");
scanf("%s",&str);
strlen(str); return something }

Can you please explain why?

That's it.
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Feb 10 '06 #2
Nelu wrote:
Newbie wrote:
int main() use: int main(void)
{
char *str;
printf("Enter a string to reverse\n");
scanf("%s",&str);

Here's the problem. str points to somewhere in memory
but you have no idea where and whether or not that area
belongs to you. You need to allocate memory (dynamically).
You can run into the problem again if you read more
characters than you allocated.


Another problem, as big as not allocating any space for the string, is
that scanf("%s",...) requires `char*`, and you give it `char**`. You
(OP) used `&str` which yields the address of `str` variable, /not/ the
string it points to (even if one exists).

You don't seem to have high enough compiler warning setting, or you
ignored the one you got. My gcc complained...

The badness can be masked also if you just quickly test with the string
that will fit into the sizeof(char*) on your system (it would break
later, when you try to use now broken pointer).
strlen(str);

return something.
}

if I use char str[20] it runs fine.

Well, yes, because you str[20] is automatically
allocated and you tell it exactly how much memory
you want. To dynamically allocate memory you need
to use a pointer and allocate memory for it (malloc & co).
int main()

int main(void)
{
char str[20];
printf("Enter a string to reverse\n");
scanf("%s",&str);
strlen(str);

return something
}

Can you please explain why?


Here, you have exactly the same problem as above. Use `str` and not
`&str` for scanf(). Don't get confused by other types. Revisit your
textbook sections on pointers and C strings.
That's it.


Indeed.

--
BR, Vladimir

"OK, now let's look at four dimensions on the blackboard."
-- Dr. Joy

Feb 10 '06 #3
elu wrote:
You can run into the problem again if you read more
characters than you allocated.


I agree with you on that.
But the codes below works even if you input more than 10 characters?
Why?

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char *str;
str = new char[10];
printf("Enter a string to reverse\n");
scanf("%s",str);
cout << strlen(str)<< endl;
system("pause");
return 0;
}

Feb 10 '06 #4
ge*****@hotmail.com wrote:
elu wrote:
You can run into the problem again if you read more
characters than you allocated.


I agree with you on that.
But the codes below works even if you input more than 10 characters?
Why?

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char *str;
str = new char[10];
printf("Enter a string to reverse\n");
scanf("%s",str);
cout << strlen(str)<< endl;
system("pause");
return 0;
}


I don't know, it's not C!
Please post C++ questions to comp.lang.c++

<OT>
I don't see why would it work for more characters than you allocate.
</OT>

--
BR, Vladimir

Goto, n.:
A programming tool that exists to allow structured programmers
to complain about unstructured programmers.
-- Ray Simard

Feb 10 '06 #5
Vladimir S. Oka wrote:
Nelu wrote:
Newbie wrote:
int main()

use: int main(void)
{
char *str;
printf("Enter a string to reverse\n");
scanf("%s",&str);

Here's the problem. str points to somewhere in memory
but you have no idea where and whether or not that area
belongs to you. You need to allocate memory (dynamically).
You can run into the problem again if you read more
characters than you allocated.


Another problem, as big as not allocating any space for the string, is
that scanf("%s",...) requires `char*`, and you give it `char**`. You
(OP) used `&str` which yields the address of `str` variable, /not/ the
string it points to (even if one exists).

You don't seem to have high enough compiler warning setting, or you
ignored the one you got. My gcc complained...

The badness can be masked also if you just quickly test with the string
that will fit into the sizeof(char*) on your system (it would break
later, when you try to use now broken pointer).

Right, I forgot to tell him that, and that if he uses an array
& will have the same address and wouldn't crash the program.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Feb 10 '06 #6
ge*****@hotmail.com wrote:
elu wrote:
You can run into the problem again if you read more
characters than you allocated.


I agree with you on that.
But the codes below works even if you input more than 10 characters?
Why?

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char *str;
str = new char[10];
printf("Enter a string to reverse\n");
scanf("%s",str);
cout << strlen(str)<< endl;
system("pause");
return 0;
}

This is not C. It is C++.
Also, some OSes (or kernel patches) try to avoid buffer overflows
and won't crash the program. However, don't trust them. It's wrong.
I had the same problem with someone who wrote a shell on RedHat a few
years ago and it was working just fine on his machine but crashed
instantly on my slackware. When I checked the code I was stunned.
He was using pointers without allocating any memory for them. His
kernel was patched to deal with buffer overflows so, even if his
program was way wrong it was not crashing. That's not normal behavior.
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Feb 10 '06 #7
Nelu wrote:
Also, some OSes (or kernel patches) try to avoid buffer overflows
and won't crash the program. However, don't trust them. It's wrong.


This sounds absolutely /horrible/!

--
BR, Vladimir

Song Title of the Week:
"They're putting dimes in the hole in my head to see the change
in me."

Feb 10 '06 #8
Nelu said:
Vladimir S. Oka wrote:
Another problem, as big as not allocating any space for the string, is
that scanf("%s",...) requires `char*`, and you give it `char**`. You
(OP) used `&str` which yields the address of `str` variable, /not/ the
string it points to (even if one exists).

You don't seem to have high enough compiler warning setting, or you
ignored the one you got. My gcc complained...

The badness can be masked also if you just quickly test with the string
that will fit into the sizeof(char*) on your system (it would break
later, when you try to use now broken pointer).

Right, I forgot to tell him that, and that if he uses an array
& will have the same address and wouldn't crash the program.


Quibble: given char arr[20], arr (in a value context) has type char *,
whereas &arr has type char (*)[20] - these are different and incompatible
types.

More importantly: what nobody seems to have mentioned is that scanf("%s" is
a remarkably dangerous construct, in that it offers no buffer protection
whatsoever.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Feb 10 '06 #9
Richard Heathfield wrote:

Quibble: given char arr[20], arr (in a value context) has type char *,
whereas &arr has type char (*)[20] - these are different and incompatible
types. Yes. This was discussed on the list recently, I think.
More importantly: what nobody seems to have mentioned is that scanf("%s" is
a remarkably dangerous construct, in that it offers no buffer protection
whatsoever.

I think I told the OP about the problem if the buffer overflows.
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Feb 10 '06 #10
Nelu said:
Richard Heathfield wrote:

Quibble: given char arr[20], arr (in a value context) has type char *,
whereas &arr has type char (*)[20] - these are different and incompatible
types.

Yes. This was discussed on the list recently, I think.

More importantly: what nobody seems to have mentioned is that scanf("%s"
is a remarkably dangerous construct, in that it offers no buffer
protection whatsoever.

I think I told the OP about the problem if the buffer overflows.


Have you a message ID? I count four articles from you in this thread, and
none of them mentions the specific danger of unadorned %s in a scanf format
string.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Feb 10 '06 #11
Richard Heathfield wrote:
Nelu said:
Richard Heathfield wrote:
Quibble: given char arr[20], arr (in a value context) has type char *,
whereas &arr has type char (*)[20] - these are different and incompatible
types.

Yes. This was discussed on the list recently, I think.
More importantly: what nobody seems to have mentioned is that scanf("%s"
is a remarkably dangerous construct, in that it offers no buffer
protection whatsoever.

I think I told the OP about the problem if the buffer overflows.


Have you a message ID? I count four articles from you in this thread, and
none of them mentions the specific danger of unadorned %s in a scanf format
string.

In the first reply:
"You can run into the problem again if you read more
characters than you allocated."

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Feb 10 '06 #12
Nelu said:
In the first reply:
"You can run into the problem again if you read more
characters than you allocated."


Ah, thank you. I apologise for missing that.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Feb 10 '06 #13
Richard Heathfield wrote:
Nelu said:
In the first reply:
"You can run into the problem again if you read more
characters than you allocated."


Ah, thank you. I apologise for missing that.

No problem.
Now that I think of it I think I should have
written it a little better. I'm not a native English speaker
so, sometimes, I realize I could've done better a day after
saying something.
So this is actually a good thing, it makes me review things
and improve.

Thank you!
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Feb 10 '06 #14
ge*****@hotmail.com wrote:
elu wrote:
You can run into the problem again if you read more
characters than you allocated.


I agree with you on that.
But the codes below works even if you input more than 10 characters?
Why?

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char *str;
str = new char[10];
printf("Enter a string to reverse\n");
scanf("%s",str);
cout << strlen(str)<< endl;
system("pause");
return 0;
}


Ignoring the C++ part of it, which would be easy to correct, the reason
why it seems to work is bad luck. You're writing off the end of
allocated memory, possibly trashing some other allocated memory. This
is a major cause of, "How come this crashes when I do a free() on this
pointer?????"

Unfortunately, exceeding the bounds of dynamic memory isn't as likely
to provide a nice immediate crash, unlike some other forms of undefined
behavior.

Brian

Feb 10 '06 #15
Newbie a écrit :
I'm a newbie. I need to clear my pointer concepts.
A pointer is just another variable.
the code below gives segmentation fault when i use char* str;
int main()
{
char *str;
printf("Enter a string to reverse\n");
scanf("%s",&str);
Your compiler should complain about that.

This is wrong, because the type expected by scanf() with "%s" is char*.
The &str expression returns a value of type char ** (address of pointer
to char). The behaviour is indefined.
strlen(str);
str was not properly initilized. It's value is undefined. The behaviour
is undefined.

Bad, bad, bad...
}

if I use char str[20] it runs fine.
int main()
{
char str[20];
printf("Enter a string to reverse\n");
scanf("%s",&str);
Still wrong. Undefined behaviour.

You want the name of the array, because it is the address and type of
its first element (char *) as expected by "%s". Please read your C-book
again. As strange as it sounds, C-books give answers about C ...
strlen(str);
}


One more advidce. Don't follow your silly teacher on the scanf() way.
The way you are using it is wrong and dangerous. We recommend to use an
alternative to scanf() (not-a-beginner-function) as follow

Get a line with fgets()
Search and kill the '\n' with strchr()
If needed :

Convert with ssacnf() or strtol(), strtoul() or strtod() (other otions
in C99)

--
A+

Emmanuel Delahaye
Feb 10 '06 #16
ge*****@hotmail.com a écrit :
#include <cstdio>


Not C.

--
A+

Emmanuel Delahaye
Feb 10 '06 #17
Newbie a écrit :
I'm a newbie. I need to clear my pointer concepts.
A pointer is just another variable.
the code below gives segmentation fault when i use char* str;
int main()
{
char *str;
printf("Enter a string to reverse\n");
scanf("%s",&str);
Your compiler should complain about that.

This is wrong, because the type expected by scanf() with "%s" is char*.
The &str expression returns a value of type char ** (address of pointer
to char). The behaviour is indefined.
strlen(str);
str was not properly initilized. It's value is undefined. The behaviour
is undefined.

Bad, bad, bad...
}

if I use char str[20] it runs fine.
int main()
{
char str[20];
printf("Enter a string to reverse\n");
scanf("%s",&str);
Still wrong. Undefined behaviour.

You want the name of the array, because it is the address and type of
its first element (char *) as expected by "%s". Please read your C-book
again. As strange as it sounds, C-books give answers about C ...
strlen(str);
}


One more advice. Don't follow your silly teacher on the scanf() way.
The way you are using it is wrong and dangerous. We recommend to use an
alternative to scanf() (not-a-beginner-function) as follow

Get a line with fgets()
Search and kill the '\n' with strchr()
If needed :

Convert with ssacnf() or strtol(), strtoul() or strtod() (other options
in C99)

--
A+

Emmanuel Delahaye
Feb 10 '06 #18
Emmanuel Delahaye said:
ge*****@hotmail.com a écrit :
#include <cstdio>


Not C.


Chapter and verse, please.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Feb 10 '06 #19
Emmanuel Delahaye said:
Newbie a écrit :
I'm a newbie. I need to clear my pointer concepts.
A pointer is just another variable.
the code below gives segmentation fault when i use char* str;
int main()
{
char *str;
printf("Enter a string to reverse\n");
scanf("%s",&str);


Your compiler should complain about that.


Should, maybe. Must, no.

<snip>
Convert with ssacnf() or strtol(), strtoul() or strtod() (other options
in C99)


ITYM sscanf().

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Feb 10 '06 #20
Emmanuel Delahaye <em***@YOURBRAnoos.fr> writes:
Newbie a écrit :
I'm a newbie. I need to clear my pointer concepts.


A pointer is just another variable.
the code below gives segmentation fault when i use char* str;
int main()
{
char *str;
printf("Enter a string to reverse\n");
scanf("%s",&str);


Your compiler should complain about that.


The compiler can't complain if it doesn't know what arguments scanf()
expects. The program is missing the required "#include <stdio.h>".

There are a lot of subtleties, some involving the difference between
C90 and C99, but the basic rule is simple: if you're going to use any
functions declared in <stdio.h>, you *must* have a "#include <stdio.h>"
at the top of your program. Your compiler may or may not complain
if it's missing, but it's still mandatory. The same goes for functions
declared in other standard headers (e.g., malloc() in <stdlib.h>).

(Yes, you can theoretically declare the functions yourself, but
there's really no reason to do so.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 10 '06 #21
On 9 Feb 2006 22:16:21 -0800, in comp.lang.c , ge*****@hotmail.com
wrote:

(snip example of potential buffer overrun)
But the codes below works even if you input more than 10 characters?
It probably works in your IDE because you compiled in debug mode ,and
most debuggers catch that sort of error.
Why?


Luck. It could also have crashed your computer.

Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Feb 11 '06 #22
On 9 Feb 2006 21:08:33 -0800, "Newbie" <ca*************@gmail.com>
wrote:
Hi,

I'm a newbie. I need to clear my pointer concepts.

the code below gives segmentation fault when i use char* str;
int main()
{
char *str;
This pointer doesn't point anywhere yet. You need to assign it the
address of some memory that belongs to you (the address of an object
you define or the address of memory you allocate.
printf("Enter a string to reverse\n");
scanf("%s",&str);
This invokes undefined behavior. The %s requires a char*. You are
passing a char**. Even if it were to work, you would destroy the
nature of str. It is supposed to contain the address of a string, not
the string itself.
strlen(str);
Now strlen will take the data stored in str and treat it as the
address of a string. But it doesn't contain such an address. More
undefined behavior.
}

if I use char str[20] it runs fine.
int main()
{
char str[20];
printf("Enter a string to reverse\n");
scanf("%s",&str);
This has the same problem. The only reason it "works" is because on
your system char* and char(*)[20] have the same size and
representation. Even though the type is wrong, &str also points to
the first char in str.
strlen(str);
Since str is an array and not a pointer, strlen receives the address
of the first char in the array and works as expected.
}

Remove del for email
Feb 11 '06 #23
Keith Thompson said:
Emmanuel Delahaye <em***@YOURBRAnoos.fr> writes:
Newbie a écrit :
scanf("%s",&str);


Your compiler should complain about that.


The compiler can't complain if it doesn't know what arguments scanf()
expects. The program is missing the required "#include <stdio.h>".


I agree that the header is required, but not that the prototype would help a
huge deal in this situation.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Feb 11 '06 #24
Richard Heathfield <in*****@invalid.invalid> writes:
Keith Thompson said:
Emmanuel Delahaye <em***@YOURBRAnoos.fr> writes:
Newbie a écrit :
scanf("%s",&str);

Your compiler should complain about that.


The compiler can't complain if it doesn't know what arguments scanf()
expects. The program is missing the required "#include <stdio.h>".


I agree that the header is required, but not that the prototype would help a
huge deal in this situation.


Good point.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 11 '06 #25
On Thu, 09 Feb 2006 21:08:33 -0800, Newbie wrote:
Hi,

I'm a newbie. I need to clear my pointer concepts.
Well, you need to clear your head about what you think you know about
pointers. Most of it is wrong.

the code below gives segmentation fault when i use char* str;
That's lucky. It might have completely hosed your machine. Pointers are
dangerous.

You need headers of some form. Try:

#include <stdio.h> /* For printf and scanf */
#include <string.h> /* For strlen */
int main()
You mean int main(void) here.
{
char *str;
Here, str is a location that could hold a pointer to a region of memory if
one is ever assigned to it. It never gets the chance to hold anything
useful.
printf("Enter a string to reverse\n");
scanf("%s",&str);
You are feeding scanf a pointer to the variable str, a pointer to a
pointer to char. A good compiler would warn you about such things.

Secondly, the program would crash even if you had called scanf correctly.
Since str doesn't hold a pointer to any block of memory, scanf would have
tried to put characters into some random location. This is a very good way
to cause very bad things to happen.

Finally, scanf is a bad function to use in this case. The reason is, it
doesn't know when to stop stuffing characters into an area of memory.
scanf will happily overwrite your whole OS and never give you a chance to
stop it. (Modern OSes will protect themselves these days, but scanf could
easily destroy other data you would rather preserve.)
strlen(str);
This function call is quite useless. Not only are you calling it with
bogus data (the random contents of the variable str), but its job is to
compute the length of a string. Calling it without capturing its return
value is pointless.

You should return an int from main, like you said you would above. Try:
return 0;
}

if I use char str[20] it runs fine.
You mean it doesn't crash, even though it should. It still doesn't do
anything sensible.
int main()
{
char str[20];
Here, str is an array of 20 characters. That means you have given the
program a place to put the string you want to type in.
printf("Enter a string to reverse\n");
scanf("%s",&str);
It ought to crash right here. Even better, the compiler should stop and
return an error message. Are you quite sure you know how to operate your
compiler? You are /still/ passing scanf a pointer to a pointer to char,
which isn't what it wants.
strlen(str);
}

Can you please explain why?
Because you need to read a /good/ book on the C language, not whatever
crap you have now. Try "The C Programming Language", Second Edition, by
Brian Kernighan and Dennis Ritchie.

Thanks

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Feb 14 '06 #26
On Fri, 10 Feb 2006 23:20:12 +0000, Richard Heathfield wrote:
Emmanuel Delahaye said:
ge*****@hotmail.com a écrit :
#include <cstdio>


Not C.


Chapter and verse, please.


Which part of C89 or C99 mentions a header named cstdio?

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Feb 14 '06 #27
Chris Barts <ch************@gmail.com> writes:
On Fri, 10 Feb 2006 23:20:12 +0000, Richard Heathfield wrote:
Emmanuel Delahaye said:
ge*****@hotmail.com a écrit :
#include <cstdio>

Not C.


Chapter and verse, please.


Which part of C89 or C99 mentions a header named cstdio?


I think Richard's point is that "Not C" and "Not defined by the C
standard" are not synonymous. There could very well be an
application-defined or implementation-defined header named cstdio.
(In practice, of course, it's far more likely to be the standard C++
header by that name.)

The statement
foo = 42;
is C, even though the C standard doesn't define anything called "foo".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 14 '06 #28
On Tue, 14 Feb 2006 09:55:03 +0000, Keith Thompson wrote:
Chris Barts <ch************@gmail.com> writes:
On Fri, 10 Feb 2006 23:20:12 +0000, Richard Heathfield wrote:
Emmanuel Delahaye said:

ge*****@hotmail.com a écrit :
> #include <cstdio>

Not C.

Chapter and verse, please.


Which part of C89 or C99 mentions a header named cstdio?


I think Richard's point is that "Not C" and "Not defined by the C
standard" are not synonymous.


I understand that. My point was that the source was so obviously C++ that
his comment seemed inane. It even used I/O streams (the overloaded <<
operator, as I recall).
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Feb 15 '06 #29
Keith Thompson <ks***@mib.org> wrote:
Chris Barts <ch************@gmail.com> writes:
On Fri, 10 Feb 2006 23:20:12 +0000, Richard Heathfield wrote:
Emmanuel Delahaye said:

ge*****@hotmail.com a écrit :
> #include <cstdio>

Not C.

Chapter and verse, please.
Which part of C89 or C99 mentions a header named cstdio?


I think Richard's point is that "Not C" and "Not defined by the C
standard" are not synonymous. There could very well be an
application-defined or implementation-defined header named cstdio.


"cstdio", yes. <cstdio> is more dubious. It _could_ be implementation-
defined, but if so, it's still not C, but C-plus-something. An
application-specific header should be #included using quote marks, not
angle brackets, because it's undefined whether the user can even create
angle bracket headers.
The statement
foo = 42;
is C, even though the C standard doesn't define anything called "foo".


No, but in that case you should be able to point at a spot _in that
application's code_ where it is defined. You can't do that - at least
not portably - with <> headers. With a "" header you may not be able to
tell where it is, but at least you know that there is a place where that
file resides. With a <> header, you don't know even that.

Richard
Feb 17 '06 #30
On Fri, 10 Feb 2006 23:26:28 GMT, Keith Thompson <ks***@mib.org>
wrote:
Emmanuel Delahaye <em***@YOURBRAnoos.fr> writes:
char *str; scanf("%s",&str);


Your compiler should complain about that.


The compiler can't complain if it doesn't know what arguments scanf()
expects. The program is missing the required "#include <stdio.h>".

Well, it _can_. All library function names are reserved with external
linkage, and the implicit declaration in C89 is as external, so the
compiler "knows" it is the standard function and _can_ check. That
said, if you can't be bothered to invoke the necessary (see next)
declaration(s), why should the compiler do extra work to help you?
It is certainly not _required_ to.
There are a lot of subtleties, some involving the difference between
C90 and C99, but the basic rule is simple: if you're going to use any
functions declared in <stdio.h>, you *must* have a "#include <stdio.h>"
at the top of your program. Your compiler may or may not complain
if it's missing, but it's still mandatory. The same goes for functions
declared in other standard headers (e.g., malloc() in <stdlib.h>).
Very very much _should_ but not actually _must_ in all cases.
(Yes, you can theoretically declare the functions yourself, but
there's really no reason to do so.)


s/the/some of the/; exactly.

- David.Thompson1 at worldnet.att.net
Feb 20 '06 #31
On Tue, 14 Feb 2006 00:54:50 -0700, Chris Barts
<ch************@gmail.com> wrote:
On Thu, 09 Feb 2006 21:08:33 -0800, Newbie wrote:

<snip other good responses>
char str[20];


Here, str is an array of 20 characters. That means you have given the
program a place to put the string you want to type in.
printf("Enter a string to reverse\n");
scanf("%s",&str);


It ought to crash right here. Even better, the compiler should stop and
return an error message. Are you quite sure you know how to operate your
compiler? You are /still/ passing scanf a pointer to a pointer to char,
which isn't what it wants.

No. An array (like str) evaluates to a pointer, but &array is not a
pointer to pointer, rather a pointer to array (as opposed to element)
which in theory can have a different representation but in practice
doesn't (and thus 'accidentally' works). FAQ 6.12, 6.13.

- David.Thompson1 at worldnet.att.net
Feb 20 '06 #32

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

Similar topics

4
by: firegun9 | last post by:
Hello everyone, here is my program: /////////////////////////////// #include <iostream> using namespace std; void multi(double* arrayPtr, int len){ for(int i=0; i<len; i++)...
5
by: DamonChong | last post by:
Hi, I am still struggling to master C++ and is trying to understand how to achieve passing arguments using pointers. I got some questions that I like to post to the experts here, hope you can...
40
by: Foobarius Frobinium | last post by:
Please review this guide for clarity, accuracy, etc. so I can hopefully compile a very good tutorial on how to use pointers in C, including advanced topics, that is easy to follow and exposes the...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
13
by: aegis | last post by:
The following was mentioned by Eric Sosman from http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source "The alignment requirement for any type T must be a divisor of...
10
by: gk245 | last post by:
I have something like this: #include <stdio.h> main () { struct line { char write; char read;
2
by: sara | last post by:
Hi All, I think one the most difficult concepts in C++ in pointer handling. Do you know any good and complete refrence which learn me this topic well? Thanks a lot.
3
by: ali | last post by:
Hi, When I pass a pointer as an argument of a method, is it safe if I change the data pointed by the argument and return it upon completion ? For example: Object* someFunction(Object* ob)...
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.