why does pointer memory command not working [duplicate] - c

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

Related

Return string after changing it in C [duplicate]

This question already has answers here:
Function returning address of local variable error in C
(3 answers)
returning a local variable from function in C [duplicate]
(4 answers)
How to access a local variable from a different function using pointers?
(10 answers)
Closed 4 months ago.
I know, there are multiple questions about this on StackOverflow but I couldn't find a fitting solution for my problem.
I want to generate product codes (e.g. 12a, important: numbers and letters) and return them and printf. But when I do it in my main I get no result. When I printf("%s\n", pcode) in my generator function it works, but after return pcode I won't get the result.
char* generate(int num) {
char pcode[3];
... // generating code
printf("%s\n", pcode); // correct output
return pcode;
}
int main(void) {
printf("%s\n", generate(12)); // wrong output
}

why is the output contains 4 time 0's? [duplicate]

This question already has answers here:
Main calling main [duplicate]
(2 answers)
Why Output is 0000 and How? [duplicate]
(2 answers)
How does this program execute? [duplicate]
(5 answers)
understanding static int execution [duplicate]
(2 answers)
unexpected output in C (recursion)
(3 answers)
Closed 3 years ago.
why is the output contains 4 time 0's.The main call again and again until if condition become false and then it should be exit from if block.
#include <stdio.h>
int main()
{
static int i=5;
if(--i)
{
main();
printf("%d ",i);
}
}
Note the following.
the int i is static.
you are calling main reursively.
In the if condition you have a pre decrement of i
Every time you call main, the value of i will be the same as the previous call. So i will decrement each time. Since this is --i it will be 4 the first time and go to 0.
After the innermost main function returns(i==0), the printf of the main before that will be executed.
But i is static and has a value of 0. So you get 4 zeros printed for each of the main functions.

How does arrays bypass its declared length [duplicate]

This question already has answers here:
How dangerous is it to access an array out of bounds?
(12 answers)
Why doesn't my program crash when I write past the end of an array?
(9 answers)
Array index out of bound behavior
(10 answers)
No out of bounds error
(7 answers)
Closed 3 years ago.
I was making practises on the logic of arrays in c and my thought on the array length declaration was unformattable if you declare an array length to be 10 integers, that array could not keep 20 integers in memory but when I tested it I saw that I was completely wrong
int main(){
int i;
int arr[10];
for (i = 0;i<20;i++){
arr[i] = i;
}
for (i = 0;i<20;i++){
printf("%d \n",arr[i]);
}
}
I was expecting to see 10 printed numbers but it prints out 20 could someone explain how is it possible?
C and C++ don't have explicit bounds checking on array sizes. When you read/write past the end of an array, you invoke undefined behavior.
With undefined behavior, your program may crash, it may output strange results, or (as in your case) it could appear to work properly. Also, making a seemingly unrelated change such as adding an unused local variable or adding a printf for debugging can change how UB manifests itself.
Just because a program may crash doesn't mean it will.

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.

I am getting only 2 as op for the below program [duplicate]

This question already has answers here:
Is uninitialized data behavior well specified?
(7 answers)
What will be the value of uninitialized variable? [duplicate]
(6 answers)
Closed 8 years ago.
Please explain why I am getting output 2 here. My expected o/p is 5 or 7. Please throw some light. Thank you!
#include<stdio.h>
typedef enum {a=3, b, c, d, j}e;
void f(e *e1) {
printf("%ld", (int)*e1);
}
main(){
e es;
f(&es);
}
You haven't initialized es, so your program is just printing the random value that happens to be on the stack when the program runs.
You need to say something like:
e es = c;
That will give you the 5 output you seek.

Resources