Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can anybody tell me How to add two pointer variables in C++ 2

Status
Not open for further replies.

anitas

Programmer
Mar 19, 2001
10
0
0
FR

I am a beginner in c++. In c this program works . But when run in C++ it gives
an error while assigning a pointer value to an integer variable.
n1 = *(&sdobj.mark1);
n2 = *(&sdobj.mark2);

The program is given below. Can anybody explain.


#include<stdio.h>

struct student
{
char *name[5][10];
int *mark1[5];
int *mark2[5];
}sdobj;

void main()
{


int n1, n2;
int total ;

for(i=0;i<5;i++)
{
printf(&quot;Name :&quot;);
scanf(&quot;%s&quot;,sdobj.name);
printf(&quot;Mark1:&quot;);
scanf(&quot;%d&quot;,&sdobj.mark1);
printf(&quot;Mark2:&quot;);
scanf(&quot;%d&quot;,&sdobj.mark2);
}

for (i=0;i<5;i++)
{
printf(&quot;\nName: %s\n&quot;,sdobj.name);
printf(&quot;Mark1 : %d\n&quot;,*(&sdobj.mark1));
printf(&quot;Mark2 : %d\n&quot;,*(&sdobj.mark2));
n1 = *(&sdobj.mark1);
n2 = *(&sdobj.mark2);
total = n1 + n2;

printf(&quot;Total : %d\n&quot;, total);
}
}

Thank U in advance
 
Have you tried to make a cast? I mean this:

n1 = (int)*(&sdobj.mark1);
n2 = (int)*(&sdobj.mark2);

or you could try this, too:

n1 = sdobj->mark1;
n2 = sdobj->mark2;

By the way, shouldn't you access the array? you know,
mark1, mark2, ...
 
Thank U kenrae. U are right I should have coded it

printf(&quot;Mark1 : %d\n&quot;,*(&sdobj.mark1));
printf(&quot;Mark2 : %d\n&quot;,*(&sdobj.mark2));
n1 = *(&sdobj.mark1);
n2 = *(&sdobj.mark2);

Regards

anitas
 
In the above program I am not getting the aim.


If your are reading integer and that should be pointed by the first element of the array mark1 then scanf statement should be

scanf(&quot;%d&quot;, sdobj.mark1[0]);
or scanf(&quot;%d&quot;, sdobj.mark1);

The assignment should be

n1 = **sdobj.mark1;
n2 = **sdobj.mark2;

but while reading string and numbers you are not allocating memeoy. So, at runtime memory problems will come.

Explain your aim ?





 
Hello smaniraja

Thanx for U'r response. I just wanted to know if it is possible to
add two pointer variables in C++ as we do in C. Coz the program
works in C and it gives a runtime error in C++.

Thanx in advance

 
I am not getting the statement adding two pointers clarly. The following informations may be helpful for you.

Because adding two pointer [variable ] is not possible [ we should not do].
Increment and Decrement of pointer variable are possible [ we can do ].

Adding and subtracting numbers to pointer variables are possible.

comparison of two pointer variables are possible if these two pointer variable are pointing to a same array.




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top