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

Pointers -C: Doubt

Hi Buddies

Can u please help me in understanding these codes and their differences.

Code 1

void main()
{
int *p=NULL;
scanf("%d",&p);
printf("\np value: %d",p);
}


Code 2

void main()
{
int *p=NULL;
scanf("%d",p);
printf("\np value: %d",*p);
}

What will be the result of these code and their differences in storage?


Thanks and Regards
Shyam
Jul 24 '07 #1
5 1105
Meetee
931 Expert Mod 512MB
Hi Buddies

Can u please help me in understanding these codes and their differences.

Code 1

void main()
{
int *p=NULL;
scanf("%d",&p);
printf("\np value: %d",p);
}


Code 2

void main()
{
int *p=NULL;
scanf("%d",p);
printf("\np value: %d",*p);
}

What will be the result of these code and their differences in storage?


Thanks and Regards
Shyam
Hi Shyam,

First code is correct as in scanf & is preferred and it prints the value given as an output. Second code gives segmentation fault.

Regards
Jul 24 '07 #2
gpraghuram
1,275 Expert 1GB
Hi,
Both the code results in segmentation fault.
Since the P is null, u will get this.
Bay the way, have u tried executing this.?
Raghuram
Jul 24 '07 #3
Meetee
931 Expert Mod 512MB
Hi,
Both the code results in segmentation fault.
Since the P is null, u will get this.
Bay the way, have u tried executing this.?
Raghuram
Hi,

I am running the same code-1 on linux and when I enter value 5 it gives value: 5!!!!!!
It doesn't give segmentation fault!!

Regards
Jul 24 '07 #4
ilikepython
844 Expert 512MB
Hi,

I am running the same code-1 on linux and when I enter value 5 it gives value: 5!!!!!!
It doesn't give segmentation fault!!

Regards
That might be undefined behaivior. I'm not sure but I think the value the user enters in code 1 is set to the address that the pointer points to, so when you print the pointer you get the same value. I'm not sure about it though.
Jul 24 '07 #5
Banfa
9,065 Expert Mod 8TB
Hi,

I am running the same code-1 on linux and when I enter value 5 it gives value: 5!!!!!!
It doesn't give segmentation fault!!

Regards
I suspect that on your C/C++ impementation sizeof(int) == sizeof(int *).

Code 1 erroneously stores the integer value input in the location of a variable of type int * and then treats that variable as it it were an int when it prints it out.

It doesn't give a segmentation fault but is really quite wrong and the code is likely to stop working properly on any system where sizeof(int) != sizeof(int *).
Jul 24 '07 #6

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

Similar topics

138
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
19
by: s.subbarayan | last post by:
Dear all, I had this following doubt,while java is able to carryon with out pointers why C language cant be modified to remove pointer?Hows java able to do this with out pointers? I jus plead...
4
by: Deep | last post by:
I'm in doubt about what is smart pointer. so, please give me simple description about smart pointer and an example of that. I'm just novice in c++. regards, John.
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
18
by: William Ahern | last post by:
I'm writing an unpack function which uses "c", "h", "l", "q" format tokens to specify the type of the integral object pointer. To support fixed-width types, a la <stdint.h>, another format...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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...

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.