Solved

Basic C doubt

Posted on 2004-08-26
4
633 Views
Last Modified: 2012-05-05
hi,
 i wrote the below program to learn about pointers(I just started learnning pointers, Infact i just started learnning C)>)
#include<stdio.h>
#include<conio.h>

void main()
{
float a=13.5;
float *b,*c,*d;
b=&a;
c=b;
d=c;
printf ("\n%u,%u,%u %u",&a,d,c,b);
printf ("\n%f,%f,%f,%f,%f",a,*(&a),*&a,*b,*c);
getch();
getch();
}
I got the o/p as... 65522,65522,65522
13.5,13.5,13.5,13.5,13.5
My first doubt:
when i wrtie c=b, what i expect was address of b should go to c, but this was not the case here, am i right?
aslo i thought for d=c, address of c should go to d, but this was not the case here
Could anyone explain what exactly happend here.
Doubt#2:
printf ("\n%u,%u,%u %u",&a,d,c,b);
when i changed the above printf to printf ("\n%u,%f,%f %f",&a,*d,*c,b);
complier dosent stop for getch(); and when i checked the output screen its shows; floating point error Domain Abonormal program termination.
Could anyone explain this pleaseee.
please considor i just started learnning C, so please explain in a simple way,
thanks in advance.
0
Comment
Question by:deepthiji
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 23

Assisted Solution

by:brettmjohnson
brettmjohnson earned 100 total points
ID: 11909308
a is a variable of type float.
b, c, and d are pointers to floats - variables containing the address of a floating point number.

b=&a;      // This makes b point to a - float pointer b now points at float variable a.
c=b;      // The value of b is the address of a, assign that same value to c.  Now c also points to a.
d=c;      // Ditto.

> when i changed the above printf to printf ("\n%u,%f,%f %f",&a,*d,*c,b);
> floating point error Domain Abonormal program termination.

The format string tells printf how to interpret the data it gets passed on its stack.
The final parameter (b) is a pointer, however your format string says it is a float.
When printf tries to interpret the address as a floating point value, it fails because
the data does not conform to the IEEE double floating point format.

0
 
LVL 4

Assisted Solution

by:pankajtiwary
pankajtiwary earned 150 total points
ID: 11909987
Please find below my comments on the code and also your inferences.

#include<stdio.h>
#include<conio.h>

void main()
// Please learn to write int main() instead of void. This is wrong.
{
float a=13.5;     // a memory location with address 65522 is chosen and it is given a value of 13.5
float *b,*c,*d;  // 3 variables which can only keep address of floats
b=&a;        // b is assigned with the address of a that is 65525. in other words, the value of b is 65525. It may be allocated anywhere on the stack with XYZ address.
c=b;       // c is allocated some where we are not concerned, but it contains whatever is the vaue of b i.e. c is also now 65522
d=c;     // similarly d is now 65522
printf ("\n%u,%u,%u %u",&a,d,c,b);
printf ("\n%f,%f,%f,%f,%f",a,*(&a),*&a,*b,*c);
getch();
getch();
}
I got the o/p as... 65522,65522,65522
13.5,13.5,13.5,13.5,13.5
My first doubt:
when i wrtie c=b, what i expect was address of b should go to c, but this was not the case here, am i right?
>> No, when you write c=b, the content of b that is the address of a that is 65522 is assigned to c. If you want to assign the address of b to c, you should write c=&b. This is what the '&' operator does.
aslo i thought for d=c, address of c should go to d, but this was not the case here
Could anyone explain what exactly happend here.
>> Same thing here. the content of c that is 65522 is assigned to d

Doubt#2:
printf ("\n%u,%u,%u %u",&a,d,c,b);
when i changed the above printf to printf ("\n%u,%f,%f %f",&a,*d,*c,b);
>> The last formatter is given as %f, so it expects a floating point argument but you are passing b which is not a float but a float* which are two different things.

complier dosent stop for getch(); and when i checked the output screen its shows; floating point error Domain Abonormal program termination.
Could anyone explain this pleaseee.
please considor i just started learnning C, so please explain in a simple way,
thanks in advance.
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 250 total points
ID: 11910604
Hi Deep,

>when i wrtie c=b, what i expect was address of b should go to c, but this was not the case here, am i right?
>aslo i thought for d=c, address of c should go to d, but this was not the case here
>Could anyone explain what exactly happend here.

Let us check the code a bit

>float a=13.5;
On seeing this statement, compiler will allocate a float variable called "a" somewhere in memory and put contents 13.5 in that memory location

let us assume this variable was stored at memory location 100 ... so our memory looks like this

                  ___________
                 |    13.5        | "a" - float
                  ----------------
                        100

>float *b,*c,*d;
On seeing this statement, compiler will allocate memory for 3 float pointer variables but the contents of these variables will be undefined. let us assume that these variables were created at memory location 110, 114, and 118 respectively

Now our memory looks like this

                  ___________              ___________         ___________           ___________  
                 |    13.5        | "a"       |    xxxx        | "b"  |    xxxx        | "c"    |    xxxx        | "d"
                  ----------------             ----------------        ----------------           ----------------
                        100                             110                      114                         118


>b=&a;               <----- 1
this statements reads, assign to b, the address of a. The net effect of this statement will be

                  ___________              ___________         ___________           ___________  
                 |    13.5        | "a"       |    100         | "b"  |    xxxx        | "c"    |    xxxx        | "d"
                  ----------------             ----------------        ----------------           ----------------
                        100                             110                      114                         118

>c=b;                 <------ 2
this statement reads, assign to c, the value held in b. The net effect of this statement will be

                  ___________              ___________         ___________           ___________  
                 |    13.5        | "a"       |    100         | "b"  |     100        | "c"    |    xxxx        | "d"
                  ----------------             ----------------        ----------------           ----------------
                        100                             110                      114                         118
>d=c;               <----------3
this statement reads, assign to d, the value held in c. The effect of this statement will be

                  ___________              ___________         ___________           ___________  
                 |    13.5        | "a"       |    100         | "b"  |     100        | "c"    |     100        | "d"
                  ----------------             ----------------        ----------------           ----------------
                        100                             110                      114                         118

as you see, now all three pointers, (b,c and d) hold address of a. This should explain you the output of your program


now to revisit your questions

>when i wrtie c=b, what i expect was address of b should go to c, but this was not the case here, am i right?
>aslo i thought for d=c, address of c should go to d, but this was not the case here

For address of b to be transferred to c, you need a statement like
c = &b;

This reads, assign to c the address of b.

However, there is a complication. c is of type float* and b is also of type float*. When you write c = &b, you expect c to point to float pointer. Thus the value in c is a pointer to a float pointer and therefore, the data type of c should be float ** and not float *.
If you find this a bit difficult to understand, do not worry, it takes a while to comprehend, read it a few more times.


>Doubt#2:
>printf ("\n%u,%u,%u %u",&a,d,c,b);
>when i changed the above printf to printf ("\n%u,%f,%f %f",&a,*d,*c,b);
>complier dosent stop for getch(); and when i checked the output screen its shows; floating point error Domain Abonormal
>program termination.

You need to extra careful while dealing with printf and scanf. These statements do not perform an sort of checking between the type of data format specified and type of data supplied. You can supply %d as format specifier and pass a char for printing. The effect will be that printf will try to retrieve an int from the argument stack (say 4 bytes) while there is only a char available (1 byte). The results are unpredictable.

It is the responsibility of the programmer to ensure that format specifier matches the data argument supplied.

As a side note, use %p format specifier to print a pointer.

good luck
sunnycoder
0
 
LVL 3

Author Comment

by:deepthiji
ID: 11914937
WOW, thats was an awesome work guys,
thanks a lot for the help and sorry for the delay in responding.
You all three guys deserve 500 points each, but i all i have is 500>)
i will split the points
thanks once again, thanks a looot
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question