473,503 Members | 11,508 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer and assignment question

Hi, in this statement:

int num1, *ptr;
num1=50;
ptr=&num1;
*ptr = (*ptr+25);
The indirection operator in (*ptr+25) says take the value that is held
in the address pointed to *ptr (which is the address of num1 the value
being 50).

So what does the *ptr on the left side of the assignment do? Is it
saying take the result of the right side (75) and assign it to the
location in memory pointed to by *ptr (or &num1)?

Oct 3 '05 #1
2 1186
ne***********@gmail.com wrote:
Hi, in this statement:

int num1, *ptr;
num1=50;
ptr=&num1;
*ptr = (*ptr+25);
The indirection operator in (*ptr+25) says take the value that is held
in the address pointed to *ptr (which is the address of num1 the value
being 50).

So what does the *ptr on the left side of the assignment do? Is it
saying take the result of the right side (75) and assign it to the
location in memory pointed to by *ptr (or &num1)?


Yes. 'num1' should after that have the value 75.

V
Oct 3 '05 #2
ne***********@gmail.com wrote:

Hi, in this statement:

int num1, *ptr;
num1=50;
ptr=&num1;
*ptr = (*ptr+25);

The indirection operator in (*ptr+25) says take the value that is held
in the address pointed to *ptr (which is the address of num1 the value
being 50).

So what does the *ptr on the left side of the assignment do? Is it
saying take the result of the right side (75) and assign it to the
location in memory pointed to by *ptr (or &num1)?


Yes. The * is necessary, because on the left side you always implicitely
specify the address of where to store the right hand side

You dont write:

int i;
&i = 5;

meaning: store 5 at *the address of* i

you write
i = 5;

and the compiler knows that it has to take the address of the target
in contrast to j = i; where it has to take the value of i.

Consequently you also write

*ptr = ....

(ptr already holds an address, * gives you the corresponding object)
if you want to store something at the memory location where ptr points to.

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 3 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2436
by: Jim Campbell | last post by:
Suppose I wanted to a class to contain a pointer, but I didn't want to make the copy constructor or assignment operator reinitialize this pointer. (Think of a class that contains an mmapped pointer...
4
4716
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
2
4149
by: Arun Prasath | last post by:
Hi all, I have the following question regd pointer typecasting. Is the following type of pointer typecasting valid? #define ALLOC(type,num) ((type *)malloc(sizeof(type)*num)) /*begin...
35
2856
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
16
2266
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
204
12889
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
14
2185
by: blue | last post by:
Hi, all: Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13): If you really need to declare a pointer to an entire array, use something like "int (*ap);" where N is the size of...
33
5016
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
2
35491
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
8
2018
by: somenath | last post by:
Hi All, I have a doubt regarding the pointer assignment . Please have a look at the following program . #include<stdio.h> #include<stdlib.h> #define NAMESIZE 10 #define SAFE_FREE(t) if(t)\...
0
7212
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5604
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
405
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.