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

Need help in pointer

72
Hi All,

int *p = 100;

printf("%d\n", *p) //It will give segmentation fault
printf("%d\n", p) ; //It will print 100;

here *p behaving as normal variable. why so?

Thanks,
Manjunath
Dec 16 '09 #1
6 1527
Banfa
9,065 Expert Mod 8TB
Because p is a normal variable. Its type, int *, only defines the operations you can do with it, you can use additional and subtraction, you can't use multiplication or division, you can dereference, you can use it as a function parameter to a function prototyped to take another type.

I would have thought that most modern compilers would give a warning (or error) for assigning an integer to a pointer.

The segmentation fault is because 100 is not a valid address in memory but you tried to access it.
Dec 16 '09 #2
jabbah
63
Maybe the problem lies in this line:

int *p = 100;
are you sure you didnt want to:

int i = 100;
int* p = &i;
Dec 16 '09 #3
alexis4
113 100+
manjuks,

I always feel saver writing it like
(*p) = 100;
It represents the reality, something useful for complex operations. Either way, I think that all three statements you are using are not correct anyway. Did you compiled it without errors?
Generally you should pass the data of your pointer to a variable first and then use this variable in printf(). If somebody wants to understand pointers he should search for their use in functions, where they permit multiple function outputs.
Dec 16 '09 #4
sidooh
9
*p means the value stored at address 100.
p means the address 100.
Dec 17 '09 #5
whodgson
542 512MB
to Banfa
I would have thought that most modern compilers would give a warning (or error) for assigning an integer to a pointer.
Yes...I use Dev-C++ ver 4.9.9.2 which people say is out of date etc but it won`t allow the above.
Dec 17 '09 #6
Banfa
9,065 Expert Mod 8TB
When I started programming, 20 years ago, we used that exact syntax to highlight areas of the code that need looking at. Because our code compiled without warnings the warning produced by that statement insured that the issue didn't get overlooked.

In C++ it should produce an error as it is prohibited by the standard. You can't assign to a pointer like that in C++ without a reinterpret_cast.

C is more... flexible, but I think most compilers today would produce a warning but I guess they may not if the warning level is set low enough.
Dec 17 '09 #7

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

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
6
by: BuddyWork | last post by:
C function syntax is PvcsGetRevisionInfo2( HANDLE hArchive, /*Input */ unsigned char *filename, /*Input*/ unsigned char *revision, /*Input*/ unsigned char **author, /*Output*/ unsigned char...
14
by: key9 | last post by:
Hi All On coding , I think I need some basic help about how to write member function . I've readed the FAQ, but I am still confuse about it when coding(reference / pointer /instance) , so I...
23
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
7
by: nass | last post by:
hi all, i am running slackware linux and need to use some function that will will enable me to write and read from a shared mem segment.. i am using open() , to open a file, and then use mmap to...
0
by: Pucca | last post by:
Hi I'm using vs2005. I am getting a bunch of compiler warnings after I made some changes to my code that was compiling clean. I'm also getting memory errors when I run my program and it's...
8
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005, .net 2, C# for Windows application. I use DllImport so I can call up a function written in C++ as unmanaged code and compiled as a dll us vs2005. My application is able to...
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:
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.