473,405 Members | 2,185 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,405 software developers and data experts.

Another newbie question! "Segmentation Fault on char *"

Guys,

DO you know why I got segmentation fault on this piece of code?

Expand|Select|Wrap|Line Numbers
  1.   char p[]={68,69,98,112,105};
  2.   char *p1;
  3.   for (int i=0; i<5; i++)       p1[i]=p[i];
  4.   p1[5]='\0';
  5.  
  6.   cout << p1<<endl;
Thanks,

Amir.
Apr 16 '07 #1
3 1494
ilikepython
844 Expert 512MB
Guys,

DO you know why I got segmentation fault on this piece of code?

char p[]={68,69,98,112,105};
char *p1;
for (int i=0; i<5; i++) p1[i]=p[i];
p1[5]='\0';

cout << p1<<endl;

Thanks,

Amir.
It is probably because "p1" does not have a size declared. Try declaring it like "char p1[5]" or "char p1[6]" if you want the nul zero character.
Apr 16 '07 #2
JosAH
11,448 Expert 8TB
It is probably because "p1" does not have a size declared. Try declaring it like "char p1[5]" or "char p1[6]" if you want the nul zero character.
Variable p1 cannot have a size declared: it's a pointer. The error is that p1 doesn't
point to anything so storing a char at where p1 points to is shooting yourself
in the foot or worse.

The solution is to make p1 point to a bunch of chars, enough to hold all the
characters that were stored in the p array.

kind regards,

Jos
Apr 16 '07 #3
Ganon11
3,652 Expert 2GB
Mainly, youir problem comesbecause you have declared a char pointer p1, but you did not give it anything to point to. You can either make it a native chara array like ilikepython suggested, or use

Expand|Select|Wrap|Line Numbers
  1. char *p1 = new char[5]; // or the appropriate malloc() call in C
Apr 16 '07 #4

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

Similar topics

7
by: Yang Song | last post by:
HI, I am a little confused about char * and char. How would I be able to return a char* created in a function? Is new() the only way? How would I be able to return a point and a value at the same...
3
by: Steven Taylor | last post by:
Hope someone can assist. I'm trying to answer a book question. I'm going around in circles in relation to 'pointer-to-char'. Object : A short program is to be created, which involves a...
3
by: Chris Mantoulidis | last post by:
I never liked pointers really much, so I decided to stay away from them for a while. I know they're useful, so now I decided to actually learn how the work, use them, etc. Here's my question......
6
by: damian birchler | last post by:
If I run the following I get a segmentation fault: #define NAMELEN 15 #define NPERS 10 typedef struct pers { char name; int money; } pers_t;
3
by: Dakkar | last post by:
I want to make a password system like this every character will have an equal character for example a will be equal to g and b will be equal to f when person writes ab it will change automaticly...
6
by: Marcelo | last post by:
Hi, I have a declaration like the following: char tmp; char tmp2; strcpy (tmp, "58257619"); strcpy (tmp2, "");
7
by: dave | last post by:
From the FAQ: 1.32: What is the difference between char a = "string"; and char *p = "string"; ? A: The first declares an initialized and modifiable array; the second declares a pointer...
0
by: olmansju | last post by:
Hi, I'm trying to access ConceptNet's XMLRPC server but I cannot seem to get the PHP client-side method written correctly. :-( I send out the call which includes: $message = new...
7
by: owolablo | last post by:
Can anybody please tell me how to change the individual elements of a char variable. I need to parse through the string, check for a particular character and change it to something else if it is...
21
by: kumarvis | last post by:
int main () { char *str = *Aamit ; *str='R' ; printf("%s \n " ,str); } it's giving segfault why ?????
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...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
tracyyun
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.