473,388 Members | 1,230 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

operation on pointer

1. If there is a pointer, int* ptr:

int* ptr;
int a = 456;
p=&a;

(*ptr)++;
*ptr++;
++*ptr;

To me, all the above three statements mean "get value of a and increment it
by 1." But the printf() reveals that they are different operation on a.
Could someone tell me why?

2. What differences and effects ++a and a++ have in C programming? Which one
should always used?

3. Code for example:
int* p;
p = malloc(sizeof(int) * 50);
p = p + 10;
free(p);

Will free(p) free all the memory allocated by "malloc" or just part of the
memory malloc'ed, since p is not pointing the beginning of malloc'ed memory
anymore?

Thanks!

Nov 14 '05 #1
4 2100
"Aire" <ai*****@zoomit.org> wrote in
news:qk******************@bgtnsc04-news.ops.worldnet.att.net:
1. If there is a pointer, int* ptr:

int *ptr;
int a = 456;
p=&a; ptr = &a; /* ptr not p */
(*ptr)++;
*ptr++;
++*ptr;

To me, all the above three statements mean "get value of a and increment
it by 1." But the printf() reveals that they are different operation on
a. Could someone tell me why?
the first two are *identical*, drop the parens. When you print *ptr++ you
get the value of a, when you print ++*ptr you get the value of a+1, no
mystery.
2. What differences and effects ++a and a++ have in C programming? Which
one should always used?
The first expression increments the value and "gives" it to you, the
second expression "gives" you the original value and then increments it.
Neither can always be used, nothing is so absolute.
3. Code for example:
int* p;
p = malloc(sizeof(int) * 50);
p = p + 10;
free(p);

Will free(p) free all the memory allocated by "malloc" or just part of
the memory malloc'ed, since p is not pointing the beginning of malloc'ed
memory anymore?


This will be bad. You must not free a pointer modified after a malloc
call. Do this instead.

int *p = malloc(50 * sizeof *p);
int *s = p; /* save orig. p value */

/* use p
*/
p += 10;
free(s);

--
- Mark ->
--
Nov 14 '05 #2
"Mark A. Odell" <no****@embeddedfw.com> wrote in
news:Xn********************************@130.133.1. 4:
"Aire" <ai*****@zoomit.org> wrote in
news:qk******************@bgtnsc04-news.ops.worldnet.att.net:
1. If there is a pointer, int* ptr:

int *ptr;
int a = 456;
p=&a;

ptr = &a; /* ptr not p */


Crap. I goofed.
(*ptr)++; This yields the value a+1, fine.
*ptr++; This yields the value a, but then ptr points to junk
++*ptr; This first points to junk, then attempts to read it!


--
- Mark ->
--
Nov 14 '05 #3
1. If there is a pointer, int* ptr:

int* ptr;
int a = 456;
p=&a;
ptr = &a;

Following, in my understanding, is how pointers are deciphered.
Comments are welcome:

- First read the name of the identifier
- Look at it's right. If any operator follows, it operates on the pointer
- Look at it's left. If any operator precedes, it operates on the object
pointed by the pointer. Usually, a * precedes.
- If the identifier is enclosed inside parantheses, the same two rules, above,
applies recursively.

(This is heavily simplified)
(*ptr)++;
Here ptr is the identifier, and is enclosed in the parantheses. So the *ptr
is the object pointed by ptr. Hence, the ++ operator increments the object,
"a" in our case.
*ptr++;
Identifier is ptr, and on it's right is ++ (increments ptr). So it is
equivalent to
- fetch *ptr
- ptr = ptr + 1
Since, in this case ++ is postfix operator, the value *ptr retrieved first, and
then
the pointer is incremented.
++*ptr;

Nothing is on the right side. So, this statement is equivalent to ++(*ptr).
ie., fetch the object, and increment it.
To me, all the above three statements mean "get value of a and increment it
by 1." But the printf() reveals that they are different operation on a.
Could someone tell me why?

When I started learning C, the same view, as your's, used to be mine.
But, this is not so. Only first and third one increments a. The second
one increments the pointer.
2. What differences and effects ++a and a++ have in C programming? Which one
should always used?

The two statements

a++;

and

++a;

are equivalent. It is upto you to choose a style. But, there are cases
when both have different meaning.
3. Code for example:
int* p;
Someone (Dan Pop?), here, suggested this style of declaration:

int *p;
p = malloc(sizeof(int) * 50);
p = p + 10;
free(p);

Will free(p) free all the memory allocated by "malloc" or just part of the
memory malloc'ed, since p is not pointing the beginning of malloc'ed memory
anymore?

Nup. free() won't be able to release memory this way. This behaviour, I think,
is undefined. I think, you want only first 10 storage units to be retained, and
the next 40 to be released. Is it? Use realloc() for resizing the memory
allocated,
if you need to.

Thanks!


Welcome.

--
Vijay Kumar R Zanvar
My Home Page - http://www.geocities.com/vijoeyz/
Nov 14 '05 #4
On Fri, 23 Jan 2004 16:29:44 +0530, "Vijay Kumar R Zanvar"
<vi*****@hotpop.com> wrote:
1. If there is a pointer, int* ptr:

int* ptr;
int a = 456;
p=&a;
ptr = &a;


Following, in my understanding, is how pointers are deciphered.
Comments are welcome:

- First read the name of the identifier
- Look at it's right. If any operator follows, it operates on the pointer
- Look at it's left. If any operator precedes, it operates on the object
pointed by the pointer. Usually, a * precedes.


Not true. ++ptr increments the pointer, not the object pointed to.
It the presence of the * that causes the pointer to be dereferenced an
any lower precedence operators to operate on the object pointed to.
- If the identifier is enclosed inside parantheses, the same two rules, above,
applies recursively.

(This is heavily simplified)
(*ptr)++;


Here ptr is the identifier, and is enclosed in the parantheses. So the *ptr
is the object pointed by ptr. Hence, the ++ operator increments the object,
"a" in our case.
*ptr++;


Identifier is ptr, and on it's right is ++ (increments ptr). So it is
equivalent to
- fetch *ptr
- ptr = ptr + 1
Since, in this case ++ is postfix operator, the value *ptr retrieved first, and
then
the pointer is incremented.
++*ptr;


Nothing is on the right side. So, this statement is equivalent to ++(*ptr).
ie., fetch the object, and increment it.
To me, all the above three statements mean "get value of a and increment it
by 1." But the printf() reveals that they are different operation on a.
Could someone tell me why?


When I started learning C, the same view, as your's, used to be mine.
But, this is not so. Only first and third one increments a. The second
one increments the pointer.
2. What differences and effects ++a and a++ have in C programming? Which one
should always used?


The two statements

a++;

and

++a;

are equivalent. It is upto you to choose a style. But, there are cases
when both have different meaning.
3. Code for example:
int* p;


Someone (Dan Pop?), here, suggested this style of declaration:

int *p;
p = malloc(sizeof(int) * 50);
p = p + 10;
free(p);

Will free(p) free all the memory allocated by "malloc" or just part of the
memory malloc'ed, since p is not pointing the beginning of malloc'ed memory
anymore?


Nup. free() won't be able to release memory this way. This behaviour, I think,
is undefined. I think, you want only first 10 storage units to be retained, and
the next 40 to be released. Is it? Use realloc() for resizing the memory
allocated,
if you need to.

Thanks!


Welcome.


<<Remove the del for email>>
Nov 14 '05 #5

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

Similar topics

0
by: Spur | last post by:
Hi all, Suppose I want to implement a dictionary data structure of some kind, say using a simple BST. I'm wondering how to express the basic operations in the nicest manner. Especially the...
3
by: Tony Johansson | last post by:
Hello! What does this mean. For an concrete operation a publicly derived class inherits both the interface and the implementation. //Tony
2
by: s88 | last post by:
Howdy everyone: for example, the psudo code is string ABC; string DEF; string cat = ABC+"__"+DEF; what is the most popular operations in C to make the above psudo code? for example, I'll do it...
8
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
4
by: Tony | last post by:
Hello, For my application, I have a vector containing data that I need, vector<NODEnode_data; and I have a pointer of vectors which are sorted by some criterion as vector<NODE*np. I have planned...
17
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
15
by: Logan | last post by:
/* Return B from ABC I get segementation error when I execute this. Please help. */ char* getSubstring(char* larger, int a, int b) { char* smaller; int i; for(i=0; i<b; i++) {...
11
by: Logan Lee | last post by:
It is at http://211.30.198.135/pointer_operations.html. I want to get this reviewed to make sure that they are correct. I'm not sure whether copy/paste will show up correctly but the content is: ...
9
by: mickey22 | last post by:
I have created a pointer to a data in a class A and passing this pointer to a function in another class B.when I try to delete this pointer in class A I get run time exception. But if I dont delete...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.