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

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)

Related

is there any array[a,b] syntax in c? [duplicate]

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 24 days ago.
while reading about the c preprocessor I got something like array[x=y,x+1]. I haven't seen this kind of syntax in c before and after searching for many hours I didn't find anything useful.
#include <stdio.h>
int main() {
int arr[] = {5,10,15};
printf("%d %d %d",arr[0,1]);
return 0;
}
outputs:
10 1762365112 1769491896
Can someone elaborate on this?
Because of how the comma operator (,) works in C, the effect of the (questionable and woefully hard to read) array[x=y,x+1] is to first copy the value of y into x and then use x+1 as the normal (single dimensional, non-ranged, or whatever you were expecting) index in to the array.
(I do not see the need to discuss your experimental code.)
See the link to documentation of comma operator, as kindly provided in comments by Pignotto:
https://en.cppreference.com/w/c/language/operator_other#Comma_operator

Unexpected Output- long double [duplicate]

This question already has answers here:
printf and long double
(8 answers)
Closed 1 year ago.
I have written the code as follows
#include <stdio.h>
int main()
{
long double var = 3.1415926535;
printf("%.6Lf", var);
}
The output of the code is 0.000000. According to me output should be 3.141592. Please tell what mistake I am doing.
Try to use GDB compiler, run your code in steps and see where is the problem and your variable changes its value. This is how we learn program dont expect every time to others to solve your problem, this method must be the last. Take a look at this GDB tutorial and fix this problem at your own.

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

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.

What is happening in this C code? (From interview) [duplicate]

This question already has answers here:
Square of a number being defined using #define
(11 answers)
Closed 5 years ago.
I was given a variation of this C code in an interview recently, and asked what would be returned by the function.
#define fun(a) a*a
int main() {
return(fun(4+5));
}
I've run it with a printf("%d"...) in place of the return and it prints 29. No other types than "%d" showed a result. Can someone explain what is going on here?
a is expanded, not evaluated as a. So you get:
4+5*4+5
and with operator priorities; you get 4 + 20 + 5 => 29
better way (with extra outside parenthesis for protecting against outside operators too thanks to Thomas comment):
#define fun(a) ((a)*(a))
but all parenthesis of the world still don't protect against i++, function calls (with side effects, preferably)... so argument reusing in macros is always a problem (there's no syntax to declare & assign a temp variable either). Prefer inlined functions in that case.

unexpected result from printf character print [duplicate]

This question already has answers here:
Accessing arrays by index[array] in C and C++
(2 answers)
Closed 4 years ago.
#include<stdio.h>
main()
{
printf("%c\n",1-3+2["nexus"]);
}
The result is v. How does it turn out?
What does the indentation (square ones) do?
2["nexus"] is probably leading us to 3 element of the nexus considering it as an
array of characters[arr[2]=x in our case]
following is 1-3viz -2 added to x character ie added to to ascii value
and the corresponding element is v
cant exactly figure out what the square identation really mean but according to the oput this is possible way .
2["nexus"] is same as "nexus"[2], which correspond to the 2nd index or the 3rd element of this string. ( Which is 'x' here ).
In equation, 1-3+ 2["nexux"] is like 1-3+'x' that represent 'v'.
Note
When a string is declared like "nexus".
nexus[0] is 'n'
nexus[1] is 'e'
nexus[2] is 'x'
nexus[3] is 'u'
nexus[4] is 's'

Resources