operation on pointer

A

Aire

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!
 
M

Mark A. Odell

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);
 
V

Vijay Kumar R Zanvar

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.

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.

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.


Welcome.
 
B

Barry Schwarz

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)


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.


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.


Nothing is on the right side. So, this statement is equivalent to ++(*ptr).
ie., fetch the object, and increment it.


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.


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.


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

int *p;


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.



Welcome.



<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,137
Messages
2,570,797
Members
47,342
Latest member
eixataze

Latest Threads

Top