Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

New Free Tools on Dev Shed!
We're Excited to announce that Dev Shed now has 70 free tools on the site. To learn more, click here!

 Dev Shed Forums Sponsor:
#1
October 20th, 2012, 01:52 PM
 rajivprathap
Registered User

Join Date: Oct 2012
Posts: 3
Time spent in forums: 25 m 4 sec
Reputation Power: 0
A doubt in pointers

Am a absolute beginner in C programming.
My doubt is
int a=10;
int *b;

what is the difference between

b=a; and b=&a; ?

#2
October 20th, 2012, 02:07 PM
 G4143
Contributing User

Join Date: Oct 2012
Posts: 71
Time spent in forums: 1 Day 7 h 39 m 39 sec
Reputation Power: 2
What's the difference? Well

This set the pointer b = 10.
Code:
`b = a;`

And this sets the pointer b = address of a.
Code:
`b = &a;`

#3
October 20th, 2012, 02:09 PM
 rajivprathap
Registered User

Join Date: Oct 2012
Posts: 3
Time spent in forums: 25 m 4 sec
Reputation Power: 0
How can that be possible?
You cant assign a integer value to a integer pointer!!

#4
October 20th, 2012, 02:43 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,883
Time spent in forums: 3 Months 1 Day 15 h 39 m 30 sec
Reputation Power: 2199
Quote:
 Originally Posted by rajivprathap How can that be possible? You cant assign a integer value to a integer pointer!!

C is kind of a minimalist language. While many modern languages like Ada and C# will go out of their way to protect you from yourself, C has always assumed that you, the programmer, know what you are doing, so it lets you. That is a feature that was designed into C, because it was written by and for programmers who did know what they were doing.

So, then, yes you can indeed assign an integer value to an integer pointer. In some cases, such as loading an absolute address into a pointer (as you would do when writing embedded code), that is exactly what you want to be able to do. Of course, there are also the other cases where you are doing something completely stupid, but C still assumes that you know what you are doing and it will do what you tell it to do. Though a good compiler should give you warnings, which is why it is very important to read and heed warnings.

As for trying to figure out what certain code does, write a test program to test it out. For example:
Code:
```#include <stdio.h>

int main()
{
int a=10;
int *b;

printf("The contents of a is %d; the address of a is %p\n", a, &a);
printf("The address of a as an integer: %d, which in hex is 0x%X\n", &a, &a);
b = &a;
printf("The contents of *b is %d; b contains the address %p\n", *b, b);
b = a;
printf("b contains the address %p\n", b);

return 0;
}```

Here is what it outputs when I run it:
Quote:
 C:>gcc -Wall pointers.c pointers.c: In function `main': pointers.c:9: warning: int format, pointer arg (arg 2) pointers.c:9: warning: unsigned int format, pointer arg (arg 3) pointers.c:12: warning: assignment makes pointer from integer without a cast C:>a The contents of a is 10; the address of a is 0028FF44 The address of a as an integer: 2686788, which in hex is 0x28FF44 The contents of *b is 10; b contains the address 0028FF44 b contains the address 0000000A C:>

Please note the warnings on lines 9 and 12, which are:
printf("The address of a as an integer: %d, which in hex is 0x%X\n", &a, &a);
and
b = a;
respectively. Those warnings are telling you that you're trying to use a pointer as an int. It still lets you do it, but at least it warns you that you might not actually want to do that. BTW, in that last printf I did not try to dereference b, because address 10 is protected and trying to access it causes the program to crash.

Does that make it more clear?

#5
October 20th, 2012, 02:53 PM
 rajivprathap
Registered User

Join Date: Oct 2012
Posts: 3
Time spent in forums: 25 m 4 sec
Reputation Power: 0
Why will the program crash if we dereference 'b'?

#6
October 20th, 2012, 03:21 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,538
Time spent in forums: 2 Months 3 Days 8 h 51 m 52 sec
Reputation Power: 4106
Quote:
 Originally Posted by rajivprathap How can that be possible? You cant assign a integer value to a integer pointer!!

Yes you can. C allows you to do this for a very good reason. Consider that you're writing code for a system with memory mapped devices (such as an embedded system). To access a certain device on this system, you would need to write a value to a certain fixed memory location. Let's say the memory location is 1234 and you have to write 1 to that memory location to access the device latch. Then you would write something like this:
Code:
```int *ptr;
ptr = 1234;
*ptr = 1; /* Acquire device latch */
/* do something with the device here */
*ptr = 0; /* Release device latch */```

Quote:
 Originally Posted by rajivprathap Why will the program crash if we dereference 'b'?

Because b is likely pointing to memory that your program doesn't own, on your particular OS.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Last edited by Scorpions4ever : October 20th, 2012 at 03:23 PM.

#7
October 20th, 2012, 03:50 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,883
Time spent in forums: 3 Months 1 Day 15 h 39 m 30 sec
Reputation Power: 2199
Quote:
 Originally Posted by rajivprathap Why will the program crash if we dereference 'b'?

What kind of computer and C compiler are you using? Is it an MS-DOS computer with Turbo C? Since most of the word we get from the sub-continent is that their schools still make them use a 20+-year-old compiler and since Windows is getting increasingly reluctant to run anything that old, I would assume that you ideally run your Turbo C on an MS-DOS computer, just as I did professionally back around 1990 -- well, I had started learning on a copy of Turbo C I had bought a few years earlier, but most of my professional work up to 1996 was in MS-DOS using Borland C++; Windows 3.1 ran on top of MS-DOS.

In MS-DOS, only one program could run at a time, though there were some programs that allowed some form of multi-tasking (eg, Windows 3.x). The programs you wrote under MS-DOS had access to everything on that computer. For example, it was common for a program to capture interrupts by modifying the Interrupt Vector Table at the very beginning of memory, location 0000:0000, where NULL would point to. And since any program could write to any location in memory, you could easily overwrite part of the operating system which would cause the computer to either "freeze up" (become unresponsive) or crash (go into a spontaneous re-boot). Nothing on that system was safe from an ill-behaved program.

That is not how an operating system is supposed to work. For example, in UNIX (which predates MS-DOS by about a decade), multiple programs, called processes, are running concurrently (with preemptive multitasking, they are each given a turn to run for a very brief time). Each process is given its own resources which includes its own portion of memory. Each process is only allowed to access its own memory space and is forbidden to attempt to access any memory locations outside that space. This is to protect the other processes and the computer itself from an ill-behaved or malignant process. When a process does try to access outside its own space, the operating system terminates that program with extreme prejudice with either an ACCESS or a SEGMENTATION FAULT error.

Study the genealogy of Windows. While still working with Windows 3.x, Microsoft had acquired another company's operating system and transformed that into the "new technology" Windows NT. Then as they came out with further releases of NT, Microsoft developed Windows 95, then Windows 98, Windows 98SE, and finally Windows ME, a rushed released that marked the end of that line. Then Windows NT became Windows 2000, then Windows XP, then Vista, and now Windows 7 and soon Windows 8.

What Microsoft did with the Windows NT line and the Windows 95 line was to make Windows itself the operating system. It also introduced into its products the well-known technology of preemptive multitasking and the concept of protecting the computer and other users from an ill-behaved or malignant process; eg, a process that tries to access memory outside its own allotted memory space.

And that is why trying to access memory location 10 (0x0000000A) will cause your program to crash. Unless you are running it under MS-DOS.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > A doubt in pointers

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |