Aman
can anyone tell me that how i can correct this error
Aman
Aman
https://hastebin.com/fepokokawa.cpphttps://hastebin.com/fepokokawa.cpp
olli
a 2d array is an array of pointers, a 3d array is an array of pointers of pointers and so on
int array[2][2]; is not an array of pointers, it has a continuous memory layout (as @bdbai mentioned before)
Vlad
'Cause it does i * width + j under the hood
布丁
so use std::array if possible😁
布丁
straightforward, easier to use
olli
gcc creates the same assembly for both of these int a1[][3] = { {1, 2, 3}, {4, 5, 6} }; int arr[] = { 1, 2, 3, 4, 5, 6 };
Abd_jack
S
Daniele
writing arr[i][j] is same as writing *(*(arr+i))+j)
there are 3 closing brackets and 2 opening
Daniele
aka matrix
Vlad
isn't this an array of arrays?
It is. However it is contiguous in memory.
Vlad
If you've got some int** it won't be.
olli
If you've got some int** it won't be.
that's not an array of arrays tho
Daniele
that's not an array of arrays tho
yes of course, that's a pointer to a pointer
Daniele
pointer ≠ array
Vlad
that's not an array of arrays tho
Yes but it allows double indexing on dynamic memory.
Vlad
Or not dynamic if you wish to set up all these pointers.
Aman
missing return
@olli thank u
Anonymous
How to pass 2d array on c in a function? I looked or up on geeks for geeks but Hacker rank shows error that way
oh you can also do void func(void *arr, size_t rows, size_t cols) { int (*arr_real)[cols] = arr; for (size_t i = 0; i < rows; ++i) { for (size_t j = 0; j < cols; ++j) { // do something with arr_real[i][j] } } }
Anonymous
this isn't type safe. but C users live on the edge anyway
Anonymous
Vlad
no
well cols ain't static
Anonymous
well cols ain't static
i'm declaring a pointer there
Vlad
i'm declaring a pointer there
It wouldn't work without vla.
Anonymous
this is also how you would declare 2D arrays using malloc int (*arr)[cols] = malloc(sizeof(*arr) * rows);
Anonymous
if this was illegal, the only option would be a lookup table
Vlad
yes it would
nope :P. https://godbolt.org/z/afxfKj
Anonymous
Anonymous
ye even the malloc one throws warning
Vlad
Also why pass void* and not int*?
Anonymous
Also why pass void* and not int*?
casting to int * would be undefined behaviour. casting to void * and casting back to original type is well defined.
Anonymous
&arr[0][0]
oh ye that followed by your (i * length + j) would work. or arr[0]
Anonymous
but the malloc
Anonymous
everything i knew was a lie
Sherlock
int findbeautiful(int n,int **ar,int d,int startindex)
Sherlock
the array decay is not a problem this way
Anonymous
int findbeautiful(int n,int **ar,int d,int startindex)
this is 100% undefined behaviour unless you are passing a lookup table as argument
Anonymous
int a[2][3] cannot be cast to int **
Sherlock
lookup table?
Vlad
lookup table?
Yep. Array of pointers
Vlad
Needed to access data
Anonymous
lookup table?
int **arr = malloc(something) for each i in [0, rows) arr[i] = malloc(something)
Anonymous
it works
undefined behaviour
布丁
it works
crash when you dereference it twice
Sherlock
n is the number of columns
Sherlock
the number of rows is 2 in that case so no need to pass it
Vlad
Well, unless you need sparse 2d array don't even bother with int**.
Vlad
Use int* and i * width + j instead
Sherlock
ok
Anonymous
Wrong ans or time exceed ?
oh i figured out what was wrong with my solution https://www.codechef.com/viewsolution/37393980 in case 0: should have been temp, not temp + 1 can you submit the changed version and see if it works? i deleted my account and apparently i can't register again with the same username.
Anonymous
Wtf
What's your opinion on this ?
klimi
What's your opinion on this ?
Something along "what the fuck"
Anonymous
I mean to say here anyone knows the ROP ?
Vlad
I'm pretty sure return oriented programming is totally made up :P
Anonymous
Return oriented programming
Anonymous
/warn screen photo
Anonymous
Oops ! Sorry
/warn screen photo
Dima
Anonymous
oof
Anonymous
Oops ! Sorry
also, you didn't make the change in case 0
Aman
Hello admin pls remove my warn as I had send the photo of question only so that people can understand the question and answer me accordingly .I had even used the hastebin for my code.
Anonymous
This is command line argument it is use for add 2 digit for command in terminal Any buddy can explain why we will put 0 in this program if(strcmp(operation, "add")==0) ( Printf("%d",number1+number2); )
Anonymous
It is c program
Vlad
0 means equality for the strings