Very simple multi array c program - problem noob [duplicate] - c

This question already has answers here:
How dangerous is it to access an array out of bounds?
(12 answers)
Closed 2 years ago.
I can't figure out where the problem is in my program?
#include <stdio.h>
int main()
{
int a[2][2]={{1,2},{3,4}};
printf("The value of a[2][1] is %d",a[2][1]);
return(0);
}
I expected the answer to be 3, it's actually wow! 32765 wait! what!? I'm pretty confused.
Can someone help?

You don't have anything in the spot a[2][1]. I think what you meant to put is a[1][0]. Remember that the index starts at 0 not at 1.
The reason why you are getting that big number is because that number was already sitting there in that memory location. It has nothing to do with the array you created.

Related

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.

Wrong output of program [duplicate]

This question already has answers here:
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 5 years ago.
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
int main()
{ float t=1/2;
printf("\n%.4f",t);
return 0;
}
i'm trying to write a program where this section of the code is needed. The program gives wrong output and ive identified the section which is causing the problem. Instead of 0.500000 im getting 0.00000.In this program ive used constants to find whether its working or not.
cant seem to find the problem.
please help.
Thanks.
I'll link you back to Why I am getting zero in float expressions like 1/2? which has the answer.
TLDR: When you do 1/2, you're doing integer division instead of float division and getting 0. Try either 1.0/2, 1/2.0, or 1.0/2.0.

Segmentation fault Not happenign where it should [duplicate]

This question already has answers here:
Why don't I get a segmentation fault when I write beyond the end of an array?
(4 answers)
Closed 7 years ago.
I've brushing up on my C coding and going over some old exercises I did a couple of years back. I came accros a situation I am almost 100% sure it should give a segmentation fault, but instead the program runs smoothly and terminates correctly. Why is that happening?
#include <stdio.h>
int main(void){
int vals[6] = {0,0,0,0,0,0};
vals[8]++; //This should not be ok!!?
printf("Done");
return 0;
}
The behaviour of vals[8] is undefined.
It's equivalent to *(vals + 8) which is dereferencing memory outside the bounds of the array.
A "segmentation fault" is one of many things that could happen. The compiler could also eat your cat.

How to succesfully pass a 2D array to a function? [duplicate]

This question already has answers here:
Passing a 2D array to a C++ function
(18 answers)
Closed 7 years ago.
GNU GCC compiler
Here is a function: int sumsintriangle(int *a,int n)
where a is a n*n matrix .
for some purpose I added
if(*(a+(i+1)*n+(j+1)) > *(a+(i+1)*n+j))
condition to my code which was working properly ;as the condition is true for the correct values.
but for the same code when I added
sum=sum + *(a+(i+1)*n+(j+1));
then it didn't work (eg;let say sum was initially 1 and *(a+(i+1)*n+(j+1) was 4 ) then summation it should be giving me 5..but it gives me 1 as output...why??
Even ,when I called the same value *(a+(i+1)*n+(j+1)) in printf function,for just an enquiry, it is giving me 4 (original value)as output ...?
Why it is , that *(a+(i+1)*n+(j+1)) is working properly with printf but when I called it with sum it gives me incorrect value?
If you can post your function properly it could be easier to help you. but i think you have an error when you put * before your expression that will give you the content of that expression, so be sure to get the values properly.
example:
int a[]; //declare an array
a[n] // will give you the element in position 9 of the array.
*a // will give you the first element, cause an array can be treated as a pointer (indeed it is).
I hope this answer help you. If not please tell me. Good luck!
Use This code code may be its work.
*(a+(i+1))*n+(j+1)

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