array,pointer address value confusion [duplicate] - c

This question already has answers here:
How come an array's address is equal to its value in C?
(6 answers)
Closed 10 years ago.
in running the code below , i got this output
num= 2359120, addr of num=2359120, *num=10,addr of num[0]=2359120
I can't comprehend how num and &num have the same value. any help, please? i know that the name of an array is a pointer itself
#include <math.h>
#include<stdio.h>
main()
{
int num[]={10,20,30,40,50};
printf("num= %d, addr of num=%d, *num=%d,addr of num[0]=%d\n",num,&num,*num,&num[0]);
}

name of array num is same as the address of the array &num which is same as the address of the first element &num[0] and hence, your output.

Related

why does pointer memory command not working [duplicate]

This question already has answers here:
Printing pointer to integer causes segmentation fault. Why?
(7 answers)
What is a segmentation fault?
(17 answers)
What is the meaning of "wild pointer" in C?
(11 answers)
Closed 2 years ago.
I am trying to follow this basic program involving pointer into the memory.
At first We define counter to be 0 (outside main) then we make p_int to point at the same address as a counter.
But when i go into the loop for some reason it compares the register with 21 instead of 2.
after that when i have tried to change the adress and value of the pointer to a tottaly different vaue and address, it exits in an error,although it compiled well.
Whele did i go wrong?
Thanks.
int counter=0;
int main()
{
int *p_int;
p_int=&counter;
while (*p_int <2)
{
(*p_int)++;
}
p_int=(int*)0x20000002U;
*p_int=0xDEADBEEF;
return 0;
}
enter image description here
enter image description here
enter image description here
enter image description here

operations on character pointers [duplicate]

This question already has answers here:
What does 1[d=b] mean?
(3 answers)
Closed 5 years ago.
I came across a code as in below
#include <stdlib.h>
int main(){
char a[]="0123456789";
printf("%s\n",a+6[a]-2[a]);
return 0;
}
Output
456789
How does the calculation of a+6[a]-2[a] happens in printf?
Why giving just 6[a] in printf doesn't work?
printf("%s\n",6[a]);
Well, a statement like
a+6[a]-2[a]
can be re-written as
&(a[ a[6] - a[2] ])
which is simply,
use the value of a[6] (type, int) as the index in the first case
use the value of a[2] (type int) as the RHS.
The result, is a pointer, is passed to printf() as an argument to %s conversion specification.

Why can I assign an int to an array with not enough memory allocated? [duplicate]

This question already has answers here:
Why doesn't my program crash when I write past the end of an array?
(9 answers)
How dangerous is it to access an array out of bounds?
(12 answers)
Closed 5 years ago.
Here is my code:
#include<stdio.h>
int main()
{
int i, list[1];
list[0]=1;
list[1]=2;
list[2]=7;
list[55]=70;
i=sizeof list;
printf("%d %d %d %d %d Size of array is %d",list[0],list[1],list[2],list[3],list[55],i);
return(0);
}
It returns "1 2 7 4 70 Size of array is 4". Why can i assign, say 55 to list[55]. list[55] should not exist as I only gave the array list enough memory for 1 integer, right? In addition shouldn't this give me an error as list[3] doesn't exist? and if for some reason i am changing the size of the array why isn't the size 56? It comes out as 4.
So what is happening to give me the output i got?<--{main question}
[As i don't want to create a separate thread for a related question, why when i code int list[0]; the program crashes, if i am somehow changing the size from 1 to 4 shouldn't I be able to change the size from 0 to 4?]
Thanks for your help, I know this probably a stupid or obvious question.

How can we be sure that value of 'b' is 0 at beginning in this code [duplicate]

This question already has answers here:
Are global variables always initialized to zero in C? [duplicate]
(6 answers)
Closed 6 years ago.
This is a program which I came across on the net that calculates the value of 𝝅.
#include <stdlib.h>
#include <stdio.h>
long a=10000,b,c=2800,d,e,f[2801],g;//--------HERE the value of b---------//
int main()
{
printf("\nValue of b: %ld", b);
getch();
for(;b-c;)
f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)
for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);
}
The calculated value is correct. But how could the programmer be sure that the initial value of b would be 0?
It does not seem to be initialized at all!
Is there some specialty about initial value of global variables?
As pointed out in this question, global variables are initialized to 0 by default.
In your code, b is declared as a global variable.

How does this array program in C give the result 10? [duplicate]

This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 9 years ago.
I am using Ubuntu 12.04lts with the GCC compiler. This program gives the result 10. Could you anybody please describe why this program gives the result like this?
#include <stdio.h>
void main(void)
{
int arr[1] = {10};
printf("\n%d\n\n", 0[arr]);
}
arr[0] gets internally expanded to *(arr+0). Similarly 0[arr] gets expanded to *(0+arr) which points to the same thing. Hence you see 10.
In general for an array or a pointer a, a[b] always means *(a+b) where a is the starting address of the array or pointer and b is the offset. Thus, a[b] and b[a] are equivalent.
below line means arr is int type array and it has size 1 and it is initialized with 10 ie index 0 has 10
int arr[1] = {10};
then next line printf statement printing the value of arr at index 0.
printf("\n%d\n\n",0[arr]);

Resources