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 12 2018
Marmagya <ma******@epatr a.com_nospam> scribbled the following: 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 (pa*****@cc.hel sinki.fi) ---------------------------\
| 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
Marmagya wrote: 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.]
--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
In <bf**********@o ravannahka.hels inki.fi> Joona I Palaste <pa*****@cc.hel sinki.fi> writes: Marmagya <ma******@epatr a.com_nospam> scribbled the following: 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);
Fix the problem by giving %s what it wants: a pointer.
printf("The statement is : %s", a);
return 0; }
People with a clue, or even half of one, also provide the required newline
character:
printf("The statement is : %s\n", a);
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Dan Pop wrote: In <bf**********@m urdoch.hpl.hp.c om> Chris Dollin <ke**@hpl.hp.co m> writes: %s wants a string. *a is a character (viz, 'M'). BOOM today.
*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 you meant just `a`.
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.
--
Chris "iron hedgehogy" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
In <bf**********@m urdoch.hpl.hp.c om> Chris Dollin <ke**@hpl.hp.co m> writes: Dan Pop wrote:
In <bf**********@m urdoch.hpl.hp.c om> Chris Dollin <ke**@hpl.hp.co m> writes:
%s wants a string. *a is a character (viz, 'M'). BOOM today.
*a is a character, 'M' isn't!
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
On Wed, 16 Jul 2003 16:20:24 +0100, in comp.lang.c , Chris Dollin
<ke**@hpl.hp.co m> wrote: Are there any where the representation of 'M' doesn't fit into a byte?
There aren't allowed to be any. But you knew that already.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Marmagya <ma******@epatr a.com_nospam> wrote in message news:<H_******* *****@news.orac le.com>... 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. return 0; }
Regards Marmagya
pete wrote: Chris Dollin wrote: Dan Pop wrote: > In <bf**********@m urdoch.hpl.hp.c om> Chris Dollin <ke**@hpl.hp.co m> > writes: >>Dan Pop wrote: >>> In <bf**********@m urdoch.hpl.hp.c om> >>> Chris Dollin <ke**@hpl.hp.co m> >>> writes: >>>>%s wants a string. *a is a character (viz, 'M'). BOOM today. >>> >>> *a is a character, 'M' isn't! >> >>Yes it is - it just isn't a char. >> (This game brought to you by Nitpicks Anonymous.)
'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.
--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
In <bf**********@m urdoch.hpl.hp.c om> Chris Dollin <ke**@hpl.hp.co m> writes: Dan Pop wrote:
In <bf**********@m urdoch.hpl.hp.c om> Chris Dollin <ke**@hpl.hp.co m> writes:
Dan Pop wrote:
In <bf**********@m urdoch.hpl.hp.c om> Chris Dollin <ke**@hpl.hp.co m> writes:
>%s wants a string. *a is a character (viz, 'M'). BOOM today.
*a is a character, 'M' isn't!
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.
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Patchwork |
last post by:
Hi Everyone,
Please take a look at the following (simple and fun) program:
////////////////////////////////////////////////////////////////////////////
/////////////
// Monster Munch, example program
#include <list>
|
by: Proteus |
last post by:
Any help appreciated on a small perl project I need to write for
educator/teaching purposes. I have not programmed perl for some time, need
to get up to speed, maybe some kind souls hrere will help me on
this project? It looks to be a simple project, and I will start relearning
pearl, but any help appreciated!
I need to read and parse a simple text file (INPUT) containing multiple
choice quiz questions (with titles) and answers, and...
|
by: Raskolnikow |
last post by:
Hi!
I have a very simple problem with itoa() or the localtime(...).
Sorry, if it is too simple, I don't have a proper example.
Please have a look at the comments.
struct tm *systime;
time_t currentTime;
char day;
char month;
|
by: Peter |
last post by:
Hello
Thanks for reviewing my question. I would like to know how can I programmatically select a node
Thanks in Advanc
Peter
|
by: abcd |
last post by:
I am trying to set up client machine and investigatging which .net
components are missing to run aspx page.
I have a simple aspx page which just has "hello world" printed....
When I request that page like http://machinename/dir1/hellp.aspx
instead of running that page it starts downloding ...whats missing here
....why the aspx engine not running the page....
| |
by: dba_222 |
last post by:
Dear Experts,
Ok, I hate to ask such a seemingly dumb question, but I've
already spent far too much time on this. More that I
would care to admit.
In Sql server, how do I simply change a character into a number??????
In Oracle, it is:
|
by: Giancarlo Berenz |
last post by:
Hi:
Recently i write this code:
class Simple
{
private:
int value;
public:
int GiveMeARandom(void);
|
by: galiorenye |
last post by:
Hi,
Given this code:
A** ppA = new A*;
A *pA = NULL;
for(int i = 0; i < 10; ++i)
{
pA = ppA;
//do something with pA
|
by: Phillip Taylor |
last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm
having some trivial issues. In Visual Studio I create a web services
project and change the asmx.vb file to this:
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
<System.Web.Services.WebService(Namespace:="http://
wwwpreview.#deleted#.co.uk/~ptaylor/Customer.wsdl")_
|
by: Chris M. Thomasson |
last post by:
I use the following technique in all of my C++ projects; here is the example
code with error checking omitted for brevity:
_________________________________________________________________
/* Simple Thread Object
______________________________________________________________*/
#include <pthread.h>
extern "C" void* thread_entry(void*);
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |