A very simple question

M

Marmagya

Hi,
Why should the following code give me a core dump?

#include <stdio.h>

int main(){
char *a="Marmagya";

printf("The statement is : %s", *a);

return 0;
}


Regards
Marmagya
 
J

Joona I Palaste

Marmagya said:
Hi,
Why should the following code give me a core dump?
#include <stdio.h>
int main(){
char *a="Marmagya";
printf("The statement is : %s", *a);

%s expects a pointer, and then starts reading from where that pointer
points onwards until it hits a 0 byte. You're giving it a char 'M'
instead. This makes %s treat the 'M' as a pointer, and tries to read
where it points to. But which address 'M' translates to is undefined
behaviour, and it is most likely that address doesn't even belong to
you. So you get a core dump.
Fix the problem by giving %s what it wants: a pointer.

printf("The statement is : %s", a);
return 0;
}

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"You will be given the plague."
- Montgomery Burns
 
C

Chris Dollin

Marmagya said:
Why should the following code give me a core dump?

#include <stdio.h>

int main(){
char *a="Marmagya";
printf("The statement is : %s", *a);
return 0;
}

There's no "should" about it; it's undefined behaviour, so the
implementation can do what it likes, including ignoring it, printing
your string, printing a different string, or painting your toenails
purple. Luckily, your implementation falls into a core dump.

%s wants a string. *a is a character (viz, 'M'). BOOM today.

Perhaps you meant just `a`.

[Doesn't your compiler warn you about this line? GCC will, if
suitably provoked.]
 
D

Dan Pop

In said:
Fix the problem by giving %s what it wants: a pointer.

printf("The statement is : %s", a);

People with a clue, or even half of one, also provide the required newline
character:

printf("The statement is : %s\n", a);

Dan
 
C

Chris Dollin

Dan said:
In <[email protected]> Chris Dollin <[email protected]>
writes:


*a is a character, 'M' isn't!

Yes it is - it just isn't a char. (This game brought to you by Nitpicks
Anonymous.)
Perhaps he just meant a ;-) Yeah, properly referring to single letter
identifiers in plain text is a real pain.

Especially `a`. Perhaps systematically renaming his code so that the
variable was called A would have worked ...
People with OP's degree of cluelessness seldom know how to use their
compilers. When you're making your first steps in C, where should you
invest your time and effort: in your C tutorial text or in the reference
manual of your compiler? The answer is far from obvious and the net
result is that it is the advanced programmers who know how to invoke
their compilers in a newbie-friendly fashion :)

Yes. Oh, the joys of irony.
 
D

Dan Pop

In said:
Yes it is - it just isn't a char. (This game brought to you by Nitpicks
Anonymous.)

3.7.1
1 character
single-byte character
<C> bit representation that fits in a byte

There aren't many hosted implementations where the representation of an
int fits in a byte.

Dan
 
S

sunil

Marmagya said:
Hi,
Why should the following code give me a core dump?

#include <stdio.h>

int main(){
char *a="Marmagya";

printf("The statement is : %s", *a);
Because *a is a character 'M' in this case and the compiler generates
code to print the string with address as ASCII value of 'M' (0x4d) and
you are probably not allowed to access that section of memory. You can
verify this using a debugger like GDB on Linux. Anyways its supposed
to be a as %s translates to print the string whose first character is
at address specified (a) till you encounter '\0'.
Cheers,
Sunil.
 
R

Richard Heathfield

pete said:
'M' is a character constant.

And character constants are integers. An odd little corner of C, that. It's
worth blowing the dust off it every few months.
 
D

Dan Pop

In said:
Are there any where the representation of 'M' doesn't fit into a byte?

Sure: how do you expect to fit a 16-bit representation or a 32-bit
representation of M into the 8 bits of the vanilla byte?

Dan
 
C

Chris Dollin

Dan said:
Sure: how do you expect to fit a 16-bit representation or a 32-bit
representation of M into the 8 bits of the vanilla byte?

The higher-order zeros are implied in the narrower representation.
There's no necessity to have actual physical bits present to do their
job.

I'd have thought you knew this just as well as I did, so we've
probably got our wires crossed. *Is* there a hosted implementation
in which the value 'M' won't fit into a byte aka char, so that (as an
example)

void emmy(void)
{
char m = 'M';
if (m != 'M') printf( "Oh woe and catastrophe\n" );
}

calling emmy() would print out the catastrophe message?
 
A

Arthur J. O'Dwyer

The higher-order zeros are implied in the narrower representation.
There's no necessity to have actual physical bits present to do their
job.

I'd have thought you knew this just as well as I did, so we've
probably got our wires crossed. *Is* there a hosted implementation
in which the value 'M' won't fit into a byte aka char, so that (as an
example)

void emmy(void)
{
char m = 'M';
if (m != 'M') printf( "Oh woe and catastrophe\n" );
}

calling emmy() would print out the catastrophe message?

Of course not!

But I bet there're several implementations where

void enny(void)
{
char m;
m = 'M'+0; /* or even without the +0, maybe */
}

produces a compile-time warning about data truncation. :)

I think this thread has become entirely pedantic-injoke at this
point, and I don't think Dan had anything terribly profound in
mind. Depending on your definition of "profound", et cetera...

-Arthur
 
D

Dan Pop

In said:
The higher-order zeros are implied in the narrower representation.

The higher-order bits, which need not be all zeros, are *missing* in
the narrower representation. The value is the same, its representation
isn't.
There's no necessity to have actual physical bits present to do their
job.

This doesn't mean that the representation is the same. Padding bits are
part of the representation, even if they don't contribute to the value.
I'd have thought you knew this just as well as I did,

Apparently, I knew it better than you did ;-)
so we've
probably got our wires crossed. *Is* there a hosted implementation
in which the value 'M' won't fit into a byte aka char, so that (as an

We're talking about the *representation* of 'M', not about its value.
Read your own question underlined above!

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top