Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

what is error in this 2 lines of code in C....

Status
Not open for further replies.

john2020

Full Member level 5
Joined
Nov 13, 2005
Messages
292
Helped
12
Reputation
24
Reaction score
8
Trophy points
1,298
Activity points
4,911
hi all,

here's the code.....


char *p;
p="Hello";
cout<<p<<endl; // prints "Hello"
*p='M' ; // giving error


if i want to change the value at the first location ( *(p+0) ) to 'M' instead of 'H', what shud be done so that it prints "Mello";

i think as it is a string literal u cant change it,u declare it as a array and then u change it.so code is as follows:-

char p[] = "hello";
char *g = & p;
*(g + 0) = 'm';
printf("%s", p);


does anyone agree wit me?any suggestions are always welcome.
try it out,but the way u declared is wrong;take it in array and try it out u can.
 

Why don't you just do:

p(0)='M'

instead of

*p='M'

?
 

You ned to change it to: p[0] = 'M';
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top