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

Please explain this behaviour related to pointer

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 1439
Ganon11
3,652 Expert 2GB
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 Expert Mod 8TB
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 Expert 2GB
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
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...
2
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...
31
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
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...
12
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...
1
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...
285
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/...
11
by: Pranav | last post by:
The Code is compiling without Error/Bug/Exception.., What are the possibilities for this behaviour? //*************************************************************** #include<stdio.h> typedef...
0
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;
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.