473,659 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please explain this behaviour related to pointer

34 New Member
Hi All,

Here is the code which generates Segmentation Fault. Can anyone explain why the third printf fails and the first printf works?

Expand|Select|Wrap|Line Numbers
  1. main() 
  2.    char ch[7]={"Hello"}; 
  3.    char *p; 
  4.    p=ch; 
  5.    printf("Character is %c\n", *p); 
  6.    printf("Character is %s\n", p); 
  7.    printf("Character is %s\n", *p); 
Output:

Character is H
Character is Hello
Segmentation Fault(coredump)

But Why *p could not fetch the entire string when I say %s in the third printf statement? Why same *p works for first printf?

Please explain...

Thanks & Regards
Sathish Kumar
Oct 27 '07 #1
3 1460
Ganon11
3,652 Recognized Expert Specialist
When you use the %s output modifier, I assume printf expects a char* (as that would point to a series of characters, or a string). But you passed it *p, which is a character. A character is not a string, and so the printf() statement fails.

Also, why don't you have a terminating \0 character at the end of ch? I'm surprised your second printf() statement works.
Oct 27 '07 #2
Banfa
9,065 Recognized Expert Moderator Expert
Also, why don't you have a terminating \0 character at the end of ch? I'm surprised your second printf() statement works.
Ganon you banana, have you got so into C++ you have forgotten C string syntax? "Hello" implicitly includes the '\0' character at the end of it and since the array it is being assigned to is long enough for the 6 characters in that string (it is 7 characters long) it wont be a problem.
Oct 27 '07 #3
Ganon11
3,652 Recognized Expert Specialist
Ganon you banana, have you got so into C++ you have forgotten C string syntax? "Hello" implicitly includes the '\0' character at the end of it and since the array it is being assigned to is long enough for the 6 characters in that string (it is 7 characters long) it wont be a problem.
Impossible! I can't forget something I never learned!

(never learned C except from TSDN :D)
Oct 27 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1946
by: Abhish | last post by:
HI All ,:) I am a VC++ programmer,and some time My <b>Acumen</b> ask Microsofts VC++ (Visual Studio VC++ 6.0 )complier to complile my <b>senseless programs </b> ! :) <i><b> See what I have asked this time to compile !!..</b></i>
2
2164
by: Gerhard Esterhuizen | last post by:
Hi, I am observing unexpected behaviour, in the form of a corrupted class member access, from a simple C++ program that accesses an attribute declared in a virtual base class via a chain of virtual method calls. To further complicate (or perhaps simplify) matters, some compilers (GCC and MingW) produce the expected behaviour, while others (MSVS 7.1) do not. I can only offer two explanations for my observations: 1. The Microsoft...
31
2610
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
22
2095
by: Jaspreet | last post by:
I was recently asked this question in an interview. Unfortunately I was not able to answer it and the interviewer made a decision on my C strengths (or weekness) based on this single question and that was a sad end to my interview. Here is the program: #include <stdio.h> int main() { char *c ="abc";
12
1247
by: Eric Lilja | last post by:
Hello, I'm trying to help someone on a linux-oriented forum. I've taken his original code and cleaned it up, but I am still wondering about something. Here's the code: #include <stdio.h> int main(void) { int a={ {1,2,3},{4,5,6,},{7,8,9} };
1
9623
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the best Newsgroup for support with JAVA?
285
8796
by: Sheth Raxit | last post by:
Machine 1 : bash-3.00$ uname -a SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R bash-3.00$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/ specs gcc version 2.95.3 20010315 (release)
11
1393
by: Pranav | last post by:
The Code is compiling without Error/Bug/Exception.., What are the possibilities for this behaviour? //*************************************************************** #include<stdio.h> typedef struct abc_ *abc; static abc param; abc fun(void)
0
978
by: Andrew Poelstra | last post by:
On Thu, 2008-09-04 at 23:50 -0700, Pranav wrote: ITYM  #include <stdio.h> #include <stdlib.h /* Required for malloc() and free() */ typedef struct abc_ *abc;
0
8427
marktang
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...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
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 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...
1
8525
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,...
0
7356
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, 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...
0
5649
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();...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
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...

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.