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

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 20th, 2012, 12:52 PM
rajivprathap rajivprathap is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 rajivprathap User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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; ?

Reply With Quote
  #2  
Old October 20th, 2012, 01:07 PM
G4143 G4143 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 71 G4143 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 39 m 39 sec
Reputation Power: 1
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;

Reply With Quote
  #3  
Old October 20th, 2012, 01:09 PM
rajivprathap rajivprathap is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 rajivprathap User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!!

Reply With Quote
  #4  
Old October 20th, 2012, 01:43 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,143 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 36 m
Reputation Power: 1974
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?

Reply With Quote
  #5  
Old October 20th, 2012, 01:53 PM
rajivprathap rajivprathap is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 rajivprathap User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 4 sec
Reputation Power: 0
Why will the program crash if we dereference 'b'?

Reply With Quote
  #6  
Old October 20th, 2012, 02:21 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is online now
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,390 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 36 m 15 sec
Reputation Power: 4080
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 02:23 PM.

Reply With Quote
  #7  
Old October 20th, 2012, 02:50 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,143 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 36 m
Reputation Power: 1974
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC 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 
Rate This Thread:


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

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap