Pointers

S

Seven Kast USA

hi
What is use of pointerrs in c programming.is fasster than other types
by
KAST
 
D

Dave

Seven said:
hi
What is use of pointerrs in c programming.is fasster than other types
by
KAST

No, it's slower, if anything, because you have to dereference the
pointer to get at the object.

A pointer is a value stored in a computer that tells it where another
value is. A practical example of a pointer is a house address. If you
were to ask where I live, then I'd give you my address, not build a
duplicate of my house in front of you. My address is a pointer.

But you probably won't understand the use of such a device without doing
some programming.
 
J

John Bode

Seven said:
hi
What is use of pointerrs in c programming.is fasster than other types
by
KAST

This is a short question with a very long answer. I suggest you open
your favorite C reference manual to the section titled "Pointers" and
read it.

Short answer -- you use pointers (specifically, object pointers) when
you need to:

1. Write to a function parameter

void foo(int *bar)
{
*bar = 0;
}

int main(void)
{
int b = 5;
printf("before: b = %d\n", b)
foo(&b);
printf("after: b = %d\n", b);
return 0;
}

2. Pass array objects as function parameters

int sum(int *arr, size_t count)
{
size_t i;
int r = 0;

for(i = 0; i < count; i++)
r += arr;

return r;
}

int main(void)
{
int vals[5]={2,4,6,8,10};
int res;

res = sum(vals, 5);
return 0;
}

3. Track dynamically allocated memory

int main(void)
{
struct rec {int x; double y; char z[10];};

/*
** Allocate enough memory to hold 10 instances
** of struct rec.
*/
struct rec *list = malloc(10 * sizeof *list);
...
}

4. Alias an object

char arr[10] = "Hello";
char *c;

c = arr;
while (*c++ != 0)
;

5. Allow a struct or union type to reference an instance of itself

struct rec {
int value;
char *key;
struct rec *next;
};

In addition to object pointers, there are function pointers as well,
which allow you to:

1. Install callbacks -- read up on signal()
2. Implement generic algorithms -- read up on qsort()

I'm sure I've missed a couple of cases.
 
M

Malcolm

Seven Kast USA said:
hi
What is use of pointerrs in c programming.is fasster than other types
by
KAST
C pointers are what is called in other languages "indirection", or accessing
a variable by address rather than by value.
Indirection is an extremely fast operation on all current processors.
Usually it is the underlying machine operation in languages that do not
allow explicit addresses.
Pointers are used throughout extensively in C, usually for accessing arrays
or other structures that cannot be described easily by the other types.
 
W

Walter Roberson

Indirection is an extremely fast operation on all current processors.

Including the embedded processors?

I would have guessed that there are still some 8 bit processors for
embedded work that doesn't necessarily need much in the way of
pointers. (I couldn't say, though, that I'm already writting to
Santa to make *sure* that I get one in my stocking...)
 
R

ranjeet.gupta

Seven said:
hi
What is use of pointerrs in c programming.is fasster than other types
by

Most often we use the pointer instead of using the arrays, (For
Computaion). The reason behind the above is illustread below with
example

int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
int *arrayPtr = array;

now suppose you have to fetch the value at 5th index of the array.

case 1

array[4];

case 2
arrayPtr = arrayPtr + 4;
*arrayPtr;

In case 1 the compiler will derefrence the array in the following
manner

*(array + (sizeof(array[0]) * 4));

and in case 2

arrayPtr = arrayPtr + 4;

so as you can see that there is extra overhead in case 1
which is involving the multipliaction and the addition but in case 2
there is only addition.....

so pointer discard the overhead of multiplication as we see
in the case 2 (i.e Pointer)

and one more thing in case 1 you tend to call every time sizeof
also so this is also another factor. (For this I am not sure)

Hope This Helps you and let see what experts say the above.

Regards
Ranjeet
 
P

pete

Most often we use the pointer instead of using the arrays, (For
Computaion). The reason behind the above is illustread below with
example

int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
int *arrayPtr = array;

now suppose you have to fetch the value at 5th index of the array.

case 1

array[4];

case 2
arrayPtr = arrayPtr + 4;
*arrayPtr;

In case 1 the compiler will derefrence the array in the following
manner

*(array + (sizeof(array[0]) * 4));

Close.
(char *)&array[4] is
((char *)array + (sizeof(array[0]) * 4))
and in case 2

arrayPtr = arrayPtr + 4;
so pointer discard the overhead of multiplication as we see
in the case 2 (i.e Pointer)

and one more thing in case 1 you tend to call every time sizeof
also so this is also another factor. (For this I am not sure)

You can't call sizeof. It's not a function.
Whatever multiplication that you imagine taking place in array[4]
must also be taking place in (arrayPtr + 4).

(char *)(arrayPtr + 4)
is ((char *)arrayPtr + (sizeof(array[0]) * 4))
 
R

ranjeet.gupta

pete said:
Most often we use the pointer instead of using the arrays, (For
Computaion). The reason behind the above is illustread below with
example

int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
int *arrayPtr = array;

now suppose you have to fetch the value at 5th index of the array.

case 1

array[4];

case 2
arrayPtr = arrayPtr + 4;
*arrayPtr;

In case 1 the compiler will derefrence the array in the following
manner

*(array + (sizeof(array[0]) * 4));

Close.
(char *)&array[4] is
((char *)array + (sizeof(array[0]) * 4))
and in case 2

arrayPtr = arrayPtr + 4;
so pointer discard the overhead of multiplication as we see
in the case 2 (i.e Pointer)

and one more thing in case 1 you tend to call every time sizeof
also so this is also another factor. (For this I am not sure)

You can't call sizeof. It's not a function.

correct, sorry for the typo mistake.
Whatever multiplication that you imagine taking place in array[4]
must also be taking place in (arrayPtr + 4).

(char *)(arrayPtr + 4)
is ((char *)arrayPtr + (sizeof(array[0]) * 4))

So you mean that both are having the same time to execute ??
Why and what is the reason for using the pointers ?? So it
seems to me that I am asking the same question what OP asked,
If my above explanation is not correct. Please Clarify.

ranjeet
 
P

pete

Most often we use the pointer instead of using the arrays, (For
Computaion). The reason behind the above is illustread below with
example

int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
int *arrayPtr = array;

now suppose you have to fetch the value at 5th index of the array.

case 1

array[4];

case 2
arrayPtr = arrayPtr + 4;
*arrayPtr;

In case 1 the compiler will derefrence the array in the following
manner

*(array + (sizeof(array[0]) * 4));

Close.
(char *)&array[4] is
((char *)array + (sizeof(array[0]) * 4))
and in case 2

arrayPtr = arrayPtr + 4;
so pointer discard the overhead of multiplication as we see
in the case 2 (i.e Pointer)

and one more thing in case 1 you tend to call every time sizeof
also so this is also another factor. (For this I am not sure)

You can't call sizeof. It's not a function.

correct, sorry for the typo mistake.
Whatever multiplication that you imagine taking place in array[4]
must also be taking place in (arrayPtr + 4).

(char *)(arrayPtr + 4)
is ((char *)arrayPtr + (sizeof(array[0]) * 4))

So you mean that both are having the same time to execute ??

Either way could be faster.

It's possible that arrayPtr might be stored
as a base and an offset, in which case *arrayPtr
might be exactly as operationally intense as array[4].
Why and what is the reason for using the pointers ??

There's different reasons.
Most of the standard library function interfaces
need to use pointers.
 
R

ranjeet.gupta

pete said:
(e-mail address removed) wrote:

Most often we use the pointer instead of using the arrays, (For
Computaion). The reason behind the above is illustread below with
example

int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
int *arrayPtr = array;

now suppose you have to fetch the value at 5th index of the array.

case 1

array[4];

case 2
arrayPtr = arrayPtr + 4;
*arrayPtr;

In case 1 the compiler will derefrence the array in the following
manner

*(array + (sizeof(array[0]) * 4));

Close.
(char *)&array[4] is
((char *)array + (sizeof(array[0]) * 4))

and in case 2

arrayPtr = arrayPtr + 4;

so pointer discard the overhead of multiplication as we see
in the case 2 (i.e Pointer)

and one more thing in case 1 you tend to call every time sizeof
also so this is also another factor. (For this I am not sure)

You can't call sizeof. It's not a function.

correct, sorry for the typo mistake.
Whatever multiplication that you imagine taking place in array[4]
must also be taking place in (arrayPtr + 4).

(char *)(arrayPtr + 4)
is ((char *)arrayPtr + (sizeof(array[0]) * 4))

So you mean that both are having the same time to execute ??

Either way could be faster.

It's possible that arrayPtr might be stored
as a base and an offset, in which case *arrayPtr
might be exactly as operationally intense as array[4].

But, don't you think if the above senario is put into the
loop; Then definetly pointer is going to much faster then
case of array.

Futhermore many members may disagree, what I said (in the
explanation)
as it will make hardly a diffrence in both the case...... but if you
are in the loop then it will really make the diffrence in terms of
execution time.
There's different reasons.
Most of the standard library function interfaces
need to use pointers.

Sorry I did not got you, what you said above.

Regards
Ranjeet
 
P

pete

pete wrote:
(e-mail address removed) wrote:

Most often we use the pointer instead of using the arrays, (For
Computaion). The reason behind the above is illustread below with
example

int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
int *arrayPtr = array;

now suppose you have to fetch the value at 5th index of the array.

case 1

array[4];

case 2
arrayPtr = arrayPtr + 4;
*arrayPtr;

In case 1 the compiler will derefrence the array in the following
manner

*(array + (sizeof(array[0]) * 4));

Close.
(char *)&array[4] is
((char *)array + (sizeof(array[0]) * 4))

and in case 2

arrayPtr = arrayPtr + 4;

so pointer discard the overhead of multiplication as we see
in the case 2 (i.e Pointer)

and one more thing in case 1 you tend to call every time sizeof
also so this is also another factor. (For this I am not sure)

You can't call sizeof. It's not a function.

correct, sorry for the typo mistake.

Whatever multiplication that you imagine taking place in array[4]
must also be taking place in (arrayPtr + 4).

(char *)(arrayPtr + 4)
is ((char *)arrayPtr + (sizeof(array[0]) * 4))

So you mean that both are having the same time to execute ??

Either way could be faster.

It's possible that arrayPtr might be stored
as a base and an offset, in which case *arrayPtr
might be exactly as operationally intense as array[4].

But, don't you think if the above senario is put into the
loop; Then definetly pointer is going to much faster then
case of array.

Definitely? No.
The dissassembly on my compiler shows accessing the pointer
as taking an extra mov instruction.

1: /* BEGIN new.c */
2:
3: #include<stdio.h>
4:
5: int main(void)
6: {
00401020 push ebp
00401021 mov ebp,esp
00401023 sub esp,18h
7: int array[5] = {0};
00401026 mov dword ptr [array],0
0040102D xor eax,eax
0040102F mov dword ptr [ebp-14h],eax
00401032 mov dword ptr [ebp-10h],eax
00401035 mov dword ptr [ebp-0Ch],eax
00401038 mov dword ptr [ebp-8],eax
8: int *arrayPtr;
9:
10: arrayPtr = array + 4;
0040103B lea ecx,dword ptr [ebp-8]
0040103E mov dword ptr [arrayPtr],ecx
11: printf("%d\n", array[4]);
00401041 mov edx,dword ptr [ebp-8]
00401044 push edx
00401045 push offset ___xt_z(0x00413a30)+10Ch
0040104A call printf(0x004012d0)
0040104F add esp,8
12: printf("%d\n", *arrayPtr);
00401052 mov eax,dword ptr [arrayPtr]
00401055 mov ecx,dword ptr [eax]
00401057 push ecx
00401058 push offset ___xt_z(0x00413a34)+110h
0040105D call printf(0x004012d0)
00401062 add esp,8
13: return 0;
00401065 xor eax,eax
14: }
00401067 mov esp,ebp
00401069 pop ebp
0040106A ret


Futhermore many members may disagree,
what I said (in the explanation)
as it will make hardly a diffrence in both the case...... but if you
are in the loop then it will really make the diffrence in terms of
execution time.

The results will varry from one implementation to the next.
Sorry I did not got you, what you said above.

Most of the standard library functions,
especially the string functions, have pointers for parameters.
So, if you can't use pointers,
then you can't use most of the standard library.

void *memset(void *s, int c, size_t n);
void *memcpy(void *s1, const void *s2, size_t n);
void *memmove(void *s1, const void *s2, size_t n);
void *memchr(const void *s, int c, size_t n);
int memcmp(const void *s1, const void *s2, size_t n);
size_t strlen(const char *s);
char *strcpy(char *s1, const char *s2);
char *strncpy(char *s1, const char *s2, size_t n);
char *strcat(char *s1, const char *s2);
char *strncat(char *s1, const char *s2, size_t n);
char *strchr(const char *s, int c);
char *strrchr(const char *s, int c);
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
size_t strspn(const char *s1, const char *s2);
size_t strcspn(const char *s1, const char *s2);
char *strpbrk(const char *s1, const char *s2);
char *strstr(const char *s1, const char *s2);
char *strtok(char *s1, const char *s2);
 
R

ranjeet.gupta

pete said:
(e-mail address removed) wrote:

pete wrote:
(e-mail address removed) wrote:

Most often we use the pointer instead of using the arrays, (For
Computaion). The reason behind the above is illustread below with
example

int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
int *arrayPtr = array;

now suppose you have to fetch the value at 5th index of the array.

case 1

array[4];

case 2
arrayPtr = arrayPtr + 4;
*arrayPtr;

In case 1 the compiler will derefrence the array in the following
manner

*(array + (sizeof(array[0]) * 4));

Close.
(char *)&array[4] is
((char *)array + (sizeof(array[0]) * 4))

and in case 2

arrayPtr = arrayPtr + 4;

so pointer discard the overhead of multiplication as we see
in the case 2 (i.e Pointer)

and one more thing in case 1 you tend to call every time sizeof
also so this is also another factor. (For this I am not sure)

You can't call sizeof. It's not a function.

correct, sorry for the typo mistake.

Whatever multiplication that you imagine taking place in array[4]
must also be taking place in (arrayPtr + 4).

(char *)(arrayPtr + 4)
is ((char *)arrayPtr + (sizeof(array[0]) * 4))

So you mean that both are having the same time to execute ??

Either way could be faster.

It's possible that arrayPtr might be stored
as a base and an offset, in which case *arrayPtr
might be exactly as operationally intense as array[4].

But, don't you think if the above senario is put into the
loop; Then definetly pointer is going to much faster then
case of array.

Definitely? No.
The dissassembly on my compiler shows accessing the pointer
as taking an extra mov instruction.

1: /* BEGIN new.c */
2:
3: #include<stdio.h>
4:
5: int main(void)
6: {
00401020 push ebp
00401021 mov ebp,esp
00401023 sub esp,18h
7: int array[5] = {0};
00401026 mov dword ptr [array],0
0040102D xor eax,eax
0040102F mov dword ptr [ebp-14h],eax
00401032 mov dword ptr [ebp-10h],eax
00401035 mov dword ptr [ebp-0Ch],eax
00401038 mov dword ptr [ebp-8],eax
8: int *arrayPtr;
9:
10: arrayPtr = array + 4;
0040103B lea ecx,dword ptr [ebp-8]
0040103E mov dword ptr [arrayPtr],ecx
11: printf("%d\n", array[4]);
00401041 mov edx,dword ptr [ebp-8]
00401044 push edx
00401045 push offset ___xt_z(0x00413a30)+10Ch
0040104A call printf(0x004012d0)
0040104F add esp,8
12: printf("%d\n", *arrayPtr);
00401052 mov eax,dword ptr [arrayPtr]
00401055 mov ecx,dword ptr [eax]
00401057 push ecx
00401058 push offset ___xt_z(0x00413a34)+110h
0040105D call printf(0x004012d0)
00401062 add esp,8
13: return 0;
00401065 xor eax,eax
14: }
00401067 mov esp,ebp
00401069 pop ebp
0040106A ret


Futhermore many members may disagree,
what I said (in the explanation)
as it will make hardly a diffrence in both the case...... but if you
are in the loop then it will really make the diffrence in terms of
execution time.

The results will varry from one implementation to the next.
Sorry I did not got you, what you said above.

Most of the standard library functions,
especially the string functions, have pointers for parameters.
So, if you can't use pointers,
then you can't use most of the standard library.

void *memset(void *s, int c, size_t n);
void *memcpy(void *s1, const void *s2, size_t n);
void *memmove(void *s1, const void *s2, size_t n);
void *memchr(const void *s, int c, size_t n);
int memcmp(const void *s1, const void *s2, size_t n);
size_t strlen(const char *s);
char *strcpy(char *s1, const char *s2);
char *strncpy(char *s1, const char *s2, size_t n);
char *strcat(char *s1, const char *s2);
char *strncat(char *s1, const char *s2, size_t n);
char *strchr(const char *s, int c);
char *strrchr(const char *s, int c);
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
size_t strspn(const char *s1, const char *s2);
size_t strcspn(const char *s1, const char *s2);
char *strpbrk(const char *s1, const char *s2);
char *strstr(const char *s1, const char *s2);
char *strtok(char *s1, const char *s2);

--

I am sorry if I hurted you, But the thing was that i was just trying
to get
the things clear.. I never claimed I am correct, But still OP issue
reamins
open.

Thanks for your response.
ranjeet.
 

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

No members online now.

Forum statistics

Threads
474,166
Messages
2,570,907
Members
47,448
Latest member
DeanaQ4445

Latest Threads

Top