### Thread: A doubt in pointers

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
3
Rep 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. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
71
Rep Power
6
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. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
3
Rep Power
0
How can that be possible?
You cant assign a integer value to a integer pointer!!
4. 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:
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

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. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
3
Rep Power
0
Why will the program crash if we dereference 'b'?
6. 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 */```
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.
Last edited by Scorpions4ever; October 20th, 2012 at 02:23 PM.
7. 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.