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.

[SOLVED] What is wrong in this code?

Status
Not open for further replies.

agg_mayur

Member level 3
Joined
Feb 25, 2010
Messages
66
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Location
India
Activity points
1,715
int main()
{

char *p;
*p = 'D';
printf("%c\n",*p);
return 0;
}
 

code seems to be ok!!

Post the error which you are getting....
 

If i am running this code in Turbo C it is just running fine and output comes.
When i am running this code in Visual C++, it is not showing any error but nothing is coming on output.

---------- Post added at 10:57 ---------- Previous post was at 10:57 ----------

what error you are getting..

If i am running this code in Turbo C it is just running fine and output comes.
When i am running this code in Visual C++, it is not showing any error but nothing is coming on output.
 

The code is "wrong" and must be expected to cause an exception, because you are writing to an undefined memory location by using an uninitialized pointer. A popular C programming error, I think.
 

The code is "wrong" and must be expected to cause an exception, because you are writing to an undefined memory location by using an uninitialized pointer. A popular C programming error, I think.

When i am running it in a Turbo C it is not giving any error and it gives the output, while in Visual C++ it is creating problem.
I have to give some address to the pointer.
 

The code may execute in some cases, but is incorrect anyway.
Code:
int main()
{
char c;
char *p;
p=&c;
*p = 'D';
printf("%c\n",*p);
return 0;
}
 

I think it will also work if you allocate a memory space

Code:
int main()
{
char *p = malloc(sizeof(char));
*p = 'D';
printf("%c\n",*p);
return 0;
}

Alex
 

The code may execute in some cases, but is incorrect anyway.
Code:
int main()
{
char c;
char *p;
p=&c;
*p = 'D';
printf("%c\n",*p);
return 0;
}

This code is working, here pointer pointer p is pointing to the base address of c.
 

Yes, incorrect means the original code. I won't suggest a correction that doesn't work.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top