pointer


 
Thread Tools Search this Thread
Top Forums Programming pointer
# 1  
Old 11-15-2005
pointer

void main()
{
int a[]={1,2,3,4,5,6,7,8,9,10};

int *p=a;

int *q=&a[9];

cout<<q-p+1<<endl;
}

The output is 10, how?

if we give cout<<q it will print the address, value won't print....

if we give cout<<p it will print the address, value won't print....

p has the base addr; q has addr of a[9].....

q-p+1

1 100 2 102 .......10 120

so, q-p here one position moved before now it has addr of a[8]...

q-p+1 now it's again get back to the a[9]....

i expected the output as address of a[9]......

but i got the output 10...(that is value at addr )

how value will come.....

warm regards
sarwan
# 2  
Old 11-15-2005
Quote:
Originally Posted by sarwan
void main()
{
int a[]={1,2,3,4,5,6,7,8,9,10};

int *p=a;

int *q=&a[9];

cout<<q-p+1<<endl;
}
the output is actually difference between the address locations and in this case the it happens to be 10.
p => address of start of array
q => address of 9th element.

((q-p) + 1) ==> (9 - 0) + 1 ==> 10..

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Pointer confusion

Here are two programs that pass a pointer to a variable but behave differently. Shouldnt the i in second program be 0 after the function call? #include<stdio.h> void changeI(int *i) { *i = 10; } int main(void) { int i=5; printf("%d before\n", i); changeI(&i); printf("%d... (1 Reply)
Discussion started by: dragonpoint
1 Replies

2. Programming

Pointer and address

This code is to print out the program name and arguments list one by one: 1 #include<stdio.h> 2 3 void main(int argc, char *argv) 4 { 5 int iCount = 0; 6 while (iCount < argc) { 7 printf("argc:%d\t%s\n",iCount, argv); 8 ... (14 Replies)
Discussion started by: yifangt
14 Replies

3. Programming

Pointer to pointers

Hi guys, I'm trying to understand pointers in C and made a simple example and I've problems with It. Can someone help? #include <stdio.h> #include <stdlib.h> #include <assert.h> int f1(char **str_); int main(int argc, char **argv) { char *str = NULL; f1(&str); ... (3 Replies)
Discussion started by: pharaoh
3 Replies

4. Programming

C dynamic pointer

Hi, Can anyone tell me how i can declare and allocate dynamically an array of pointers to structured type?? Is declaration something like this:? struct_name ** array; (1 Reply)
Discussion started by: littleboyblu
1 Replies

5. Programming

matrix pointer

Can anyone tell me what the following statements do? float (*tab); tab=(float (*)) calloc(MAXCLASS, (MAXCLASS+1)*sizeof(float)); (3 Replies)
Discussion started by: littleboyblu
3 Replies

6. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

7. Programming

Need help in character pointer

Hi, I am trying to divide my input to different type of out puts for some other use. ex: logical_name : jkl00001 expected out put : model=jkl and num=00001 here is the code i actually written /*******************************************************************/ void... (11 Replies)
Discussion started by: jagan_kalluri
11 Replies

8. Programming

far pointer

what is far pointer in C (1 Reply)
Discussion started by: useless79
1 Replies

9. Programming

pointer problem

could any one tell why the following is showing segmentation fault while using **ptr but working fine using **a #include<stdio.h> ... (1 Reply)
Discussion started by: useless79
1 Replies

10. Programming

why we never delete a pointer twice

can u tell me the reson that why we should not delete a pointer twice.? if we delete ponter twice then what happen and why this happen Regards, Amit (2 Replies)
Discussion started by: amitpansuria
2 Replies
Login or Register to Ask a Question