473,385 Members | 2,069 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,385 software developers and data experts.

Pointer In C

Hi all,
What will the following piece of code do:
int* p;
int* q;
int i = 0;
while(i < 20){
q = (int*)&q[i];
i++;
}
i am bit confused with the 5 line of the code.
May 30 '07 #1
6 1380
Savage
1,764 Expert 1GB
Hi all,
What will the following piece of code do:
int* p;
int* q;
int i = 0;
while(i < 20){
q = (int*)&q[i];
i++;
}
i am bit confused with the 5 line of the code.

q = (int*)&q[i];

this tells to the compiler that q is a pointer to adress of *(q+i).

print it out and u will see,hexadecimal adresses all.

Savage
May 30 '07 #2
q = (int*)&q[i];

this tells to the compiler that q is a pointer to adress of *(q+i).

print it out and u will see,hexadecimal adresses all.

Savage
so for example p = 1000 and *p = 200 then q = 1000 i.e address of 200.The code is as follows:
int* p;
int* q;
int i = 0;
while(i < 20){
q = (int*)&p[i];
i++;
}
is p acting as pointer to array of ints.When i try to print q and p as:
printf("q = %u\np = %u\n", q, &p[i]);
i am getting following results:
q = 176
p = 180
q = 180
p = 184
q = 184
p = 188
q = 188
p = 192
q = 192
p = 196
q = 196
p = 200
May 30 '07 #3
so for example p = 1000 and *p = 200 then q = 1000 i.e address of 200.The code is as follows:
int* p;
int* q;
int i = 0;
while(i < 20){
q = (int*)&p[i];
i++;
}
is p acting as pointer to array of ints.When i try to print q and p as:
printf("q = %u\np = %u\n", q, &p[i]);
i am getting following results:
q = 176
p = 180
q = 180
p = 184
q = 184
p = 188
q = 188
p = 192
q = 192
p = 196
q = 196
p = 200
ok ok i got, but i have declared p as pointer to int how come i can give it a index and make it as pointer to array of ints.When i try tp print p[i] i get segmentation fault.
May 30 '07 #4
scruggsy
147 100+
ok ok i got, but i have declared p as pointer to int how come i can give it a index and make it as pointer to array of ints.When i try tp print p[i] i get segmentation fault.
You never declared an array, only two pointers to single integers.
The notation p[i] is the same as *p+i.
That makes sense if you realize that the elements of an array are adjacent to each other in memory.
However, because your pointer points to only a single integer, not an array of integers, trying to access the memory at p+i is a segmentation fault.

If p had been passed to a function as a pointer to the first element of a 20-element array, your code would work fine.
May 30 '07 #5
You never declared an array, only two pointers to single integers.
The notation p[i] is the same as *p+i.
That makes sense if you realize that the elements of an array are adjacent to each other in memory.
However, because your pointer points to only a single integer, not an array of integers, trying to access the memory at p+i is a segmentation fault.

If p had been passed to a function as a pointer to the first element of a 20-element array, your code would work fine.
But i have heard that pointer to an int same as pointer to an arrray of ints!!!
May 31 '07 #6
Savage
1,764 Expert 1GB
But i have heard that pointer to an int same as pointer to an arrray of ints!!!
char a; // allocates space for a single char

char * b; // creates space for a pointer to a char ( or an array of char)

char c [SZ]; // similar to b, must have a constant integer substitued for SZ
// however, you also allocate space for the char, c is an address like b.

char * d [SZ]; // this is interesting... like c above, its a pointer... but with a twist

char ** e; // is the eqivalent of d above... and its called a pointer to a pointer to a char

Do u know understand?

Savage
May 31 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
35
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
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
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 =...
16
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
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;
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.