473,738 Members | 4,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cant figure the output..

the code is ...
int main(void){
static int a[]={1,2,3,4,5};
static int *p[5]={a,a+1,a+2,a+3 ,a+4};
int i;
for(i=0;i<5;i++ ){ printf("%u\n",a +i);}
printf("\n%u\t% u\t%d",p,*p,*(* p));
return 0;
}
Now in this code the output which iam getting is :::
the address of p is 4 added to the address of element 5 in the array
'a'. That means p points to the address of the memory location next to
a[4] (since on my machine int is 4 bytes). But what i guessed is that
it would point same as *p that is the element a[0]. I am not able to
think how this can happen.. please reply where i am wrong in my
thinking.

Nov 14 '05 #1
4 1446
nichas <sa***********@ gmail.com> wrote:
the code is ...
#include <stdio.h> /* printf */ int main(void){
static int a[]={1,2,3,4,5};
static int *p[5]={a,a+1,a+2,a+3 ,a+4};
int i;
for(i=0;i<5;i++ ){ printf("%u\n",a +i);} printf("%x\n", (void*)(a+i)) printf("\n%u\t% u\t%d",p,*p,*(* p)); printf("\n%x\t% x\t%d\n", (void*)p, (void*)*p, *(*p)) return 0;
}
Now in this code the output which iam getting is :::
You should have included your output. Mine was:

-|8049490
-|8049494
-|8049498
-|804949c
-|80494a0
-|
-|80494a4 8049490 1
the address of p is 4 added to the address of element 5 in the array
'a'. That means p points to the address of the memory location next to
a[4] (since on my machine int is 4 bytes).
In my output it is the same. But this is, of course, a coincidence,
the address might be different.

`p' is an array (of ptrs to int), it is not a pointer, therefore
it doesn't point to anything. In the expression it decays to "a
pointer to the first element", therefore is also the address of
the second array, which happens to be adjacent to the first one
(and this is the reason for the difference of 4).
But what i guessed is that
it would point same as *p that is the element a[0].


In `*p', again `p' decays into "the pointer to the first element";
`*' and `p' together then yield an lvalue for the first element
(or simply `p[0]'). The first element is `a'; the identifier
denotes the array, but in the expression it decays into a pointer
to its first element (the value of `a' is also the address of the
first array). So `*p' is a pointer to the first element of `a' (&a[0])
(the first number in the output and the second one in the last line
are the same).

It's easy now to guess that `**p' is the same as `a[0]' (the last
output number is "1").

To sum up:

p == &p[0]
*p == p[0] == a == &a[0]
**p == a[0]

--
Stan Tobias
mailx `echo si***@FamOuS.Be dBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #2
nichas wrote:
the code is ...
int main(void){
static int a[]={1,2,3,4,5};
static int *p[5]={a,a+1,a+2,a+3 ,a+4};
int i;
for(i=0;i<5;i++ ){ printf("%u\n",a +i);}
printf("\n%u\t% u\t%d",p,*p,*(* p));
return 0;
}
Now in this code the output which iam getting is :::
the address of p is 4 added to the address of element 5 in the array
'a'. That means p points to the address of the memory location next to
a[4] (since on my machine int is 4 bytes). But what i guessed is that
it would point same as *p that is the element a[0]. I am not able to
think how this can happen.. please reply where i am wrong in my
thinking.

You need to check your textbook for
1) required headers for variadic functions (e.g. printf)
2) pointer arithmetic
3) proper specifiers for pointers with printf.

Check the following code, that differs from yours in important ways, and
see if it, along with your textbook, helps you understand what is happening:

#include <stdio.h>
int main(void)
{
static int a[] = { 1, 2, 3, 4, 5 };
static int *p[5] = { a, a + 1, a + 2, a + 3, a + 4 };
int i;
for (i = 0; i < 5; i++)
printf("a + %d = %p, &a[%d] = %p, p[%d] = %p\n", i,
(void *) (a + i), i, (void *) &a[i], i, (void *) p[i]);
printf("p = %p, *p = %p, p[0] = %p, **p = %d, *p[0] = %d\n",
(void *) p, (void *) *p, (void *) p[0], **p, *p[0]);
return 0;
}
a + 0 = b6f4, &a[0] = b6f4, p[0] = b6f4
a + 1 = b6f8, &a[1] = b6f8, p[1] = b6f8
a + 2 = b6fc, &a[2] = b6fc, p[2] = b6fc
a + 3 = b700, &a[3] = b700, p[3] = b700
a + 4 = b704, &a[4] = b704, p[4] = b704
p = b6e0, *p = b6f4, p[0] = b6f4, **p = 1, *p[0] = 1
Nov 14 '05 #3
>
#include <stdio.h>
int main(void)
{
static int a[] = { 1, 2, 3, 4, 5 };
static int *p[5] = { a, a + 1, a + 2, a + 3, a + 4 };
int i;
for (i = 0; i < 5; i++)
printf("a + %d = %p, &a[%d] = %p, p[%d] = %p\n", i,
(void *) (a + i), i, (void *) &a[i], i, (void *) p[i]); printf("p = %p, *p = %p, p[0] = %p, **p = %d, *p[0] = %d\n",
(void *) p, (void *) *p, (void *) p[0], **p, *p[0]); Can u tell me why we need to cast p, *p to the void pointers...
One more thing where is p pointing to. Its address in ur program
is b6e0 which is before a[0]. Is there any logic or p can point
anywhere.. please reply.
return 0;
}
a + 0 = b6f4, &a[0] = b6f4, p[0] = b6f4
a + 1 = b6f8, &a[1] = b6f8, p[1] = b6f8
a + 2 = b6fc, &a[2] = b6fc, p[2] = b6fc
a + 3 = b700, &a[3] = b700, p[3] = b700
a + 4 = b704, &a[4] = b704, p[4] = b704
p = b6e0, *p = b6f4, p[0] = b6f4, **p = 1, *p[0] = 1


Nov 14 '05 #4
nichas <sa***********@ gmail.com> wrote:

#include <stdio.h>
int main(void)
{
static int a[] = { 1, 2, 3, 4, 5 };
static int *p[5] = { a, a + 1, a + 2, a + 3, a + 4 };
int i;
for (i = 0; i < 5; i++)
printf("a + %d = %p, &a[%d] = %p, p[%d] = %p\n", i,
(void *) (a + i), i, (void *) &a[i], i, (void *) p[i]);
printf("p = %p, *p = %p, p[0] = %p, **p = %d, *p[0] = %d\n",
(void *) p, (void *) *p, (void *) p[0], **p, *p[0]);

Can u tell me why we need to cast p, *p to the void pointers...


Because the "%p" format specifier expects a void pointer according to
the C standard.
One more thing where is p pointing to. Its address in ur program
is b6e0 which is before a[0]. Is there any logic or p can point
anywhere.. please reply.


If you use 'p' in value context it's taken to mean the address of the
first element of the array 'p', i.e. the address of p[0], and not the
content of what's stored in p[0], which would be the address of a[0].

Regards, Jens

PS: Could you please try to avoid these "u" and "ur" abbreviations?
They make things harder to read for non-native speakers like me
and it shouldn't take you too much time to type "you" or "you're"
instead. Thank you.
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #5

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

Similar topics

7
9371
by: kecebong | last post by:
I installed php-4.3.3, mysql and apache server 2.0.47. but I can not connect to my apache server from my other computer, i use static ip for my server, set ip address for server name on httpd.conf, setup sua server on router. do i miss something here? anyone can help me to figure it out? thanks.
5
1562
by: johny | last post by:
Program details as follows: Input to the program the exam number and computer arch and programming results for 10 people. Three arrays - ExamNo, Arch and prog are used to hold these items. The First name and surname of each student should also be entered, concatenated into one string and output to an array called wholename We need to check weather the exam numbers are valid. An exam number should consist of 7 digits only Include a...
6
2036
by: Simon Mansfield | last post by:
Im trying to make a C program that takes in a date (birthday) and tells the user how many days it has been since that date. So far I have got this... It compiles ok but then crashes, with no idea why I was wondering if anyone else had any idea?? #include <stdio.h> #include <stdlib.h> #include <time.h> int main() {
3
1808
by: Kentor | last post by:
hello, im trying to make a little loop, but i cant figure it out... i have a string with a bunch of 1s and 0s in it: 110101010101111010101 .... i need to count the number of 1s divide it by 2 and make a table with 2 columns and then for every one, depending on its position in the string i need to output a word and then go to the next 1 and output a dif word.... its amenities, winter activities and summer activities for cottages that i...
2
7377
by: g35rider | last post by:
Hi, I have the following code that is giving this error, I cant simplify the code, I was just testing some theory for something we are doing and was getting an issue here. Please someone point out whats wrong with my code. class MsgData { char* data;
2
2085
by: andrewanderson | last post by:
hi can anyone help me with this prog. cant find the prob why it cant display cout<<"This is the display of your transaction"<<endl; ifstream fobj; //declare input file stream fobj.open("trans.txt"); //open file if(!fobj) //File not opened
4
1379
by: aherraiz | last post by:
Hi, I've worked with sql for some time now but i can't figure out this one. I'm pretty sure i've come across a very similar query before and i know there is a neat, good solution to this one, hope you can give me a hand. The schema (simplified) is as follows: TABLE staff ============ id | name ============
6
2478
by: WolfgangS | last post by:
Ok first off, i am a total beginner at this groups stuff and i have no clue how this works. This is probabaly the wrong group for my problem but i will post it anyways. Learning by doing right? I will post a code snippet and explain my problem. I cant seem to find a logical explanation why this problem would occur after trying to figure it out for a few hours. However I am probably just blind and/or stupid and you will realise where i go...
5
1879
by: chevon1920 | last post by:
I am trying to do my assignment but I cant figure out how to get 8 data points per line to print to a file. Here is the assignment 1. Program asks the user to enter an odd number as a BASE, using this Base it creates 48 multiples of the BASE and prints in a data file "data_3b.txt", 8-data per line. 2. Program opens this data file, scans the contents of "data_3b.txt" file and counts all the odd data, and prints them on the screen: 4 data...
0
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.