let's see if anybody on Jow Forums can actually kode
Let's see if anybody on Jow Forums can actually kode
we're not doing your CS homework for you.
this isn't cs
user, nobody here actually knows how to kode. i can tell you all about why intel is going bankrupt soon tho
>you should be able to solve this
Here, have a solution: don't do this kind of pointer bullshit.
>Do my homework
No. Reading that shit takes all of 5 minutes to learn. Do it yourself.
well this was part of my cs courses first year
but user i've known c for ages, what part of my post implies that it is for homework?
You should at least throw in one with the const first if you're going to bother, llike which one of those is const int **ptr9 = 0; or const int * const *ptr10 = 0; equivalent to.
>imperative language
that's gonna be a yikes from me
>how to write a question in as few words as possible so you can feel superior to people who don't get what its asking
>int **ptr1 = 0
mutable 2d array
>int **const ptr2 = 0
const pointer to pointer of int
>int *const *ptr3 = 0;
pointer to a const pointer of int
>int const **ptr4 = 0;
pointer to a pointer of const int
>int *const *const ptr5 = 0;
const pointer to a const pointer of int
>int const **const ptr6 = 0;
const pointer to a pointer of const int
>int const *const *ptrt7 = 0;
pointer to a const pointer of a int
>int const *const *const ptr8 = 0;
const pointer to a const pointer of a const int
expression is decomposed from right to left
ptr is the first pointer
int is the pointed data in the end
No bully if wrong
>Using 0 instead of NULL.
5
>kode
cd code
cd ..
why is the first one an array?
it could be a pointer to a pointer as well
>it could be a pointer to a pointer as well
Yes, it's just an example.
pointer to pointer to int
const pointer to pointer to int
pointer to const pointer to int
pointer to pointer to const int
const pointer to const pointer to int
const pointer to pointer to const int
pointer to const pointer to const int
const pointer to const pointer to const int
>int **ptr1
double pointer to integer
>int **const ptr2
const pointer to pointer to integer
>int *const *ptr3
pointer to const pointer to integer
>int const **ptr4
pointer to pointer to const integer
>int *const *const ptr5
const pointer to const pointer to integer
>int const **const ptr6
const pointer to pointer to const integer
>int const *const *ptr7
pointer to const pointer to const integer
>int const *const *const ptr8
const pointer to const pointer to const integer
note: const is fucking useless
t. cnile
you what user
i only answer to shit that gives money and by giving money i mean my work a my own projects so fuck off with your anime thread piece of crap
it's really easy user
well acktually 'double pointer' is technically wrong. 'A pointer to a pointer' is the right.
>double pointer
double *lmay= xxx;
>pointer to pointer
type **lmay = xxx;
by double pointer i did mean a pointer that points to another pointer, hence two pointers, hence double pointer but yes, nitpicky user
>mfw I'm a C# dev and never have to worry about any of this bullshit
>using 0 instead of null
Undefined behavior, faggot.
except there isn't anything to worry about if you aren't a brainlet
What is the purposed of a pointer to pointer?
We never used then in our course
Here's a tricky one if any one wants a challenge
const int(*a)(const float (*)[8])[8]
I thought it was easy at first sight but it made me trip. Here is a crack at it:
An array of 8 constant function pointers each taking as argument an array of 8 constant float pointers.
>"mfw my language doesn't allow me to be this explicit"
t. brainlet
is the challenge to find a compiler that this works on?
Just read them right-to-left.
E.g. const int * * const x;
Means: x is an immutable pointer to a mutable pointer to an immutable integer.
Kurisu doesn't even know what a byte is
>>int **ptr1 = 0
>mutable 2d array
That's not an array. int arr[N][M]; would be a mutable 2D array.
All of these declarations are super simple though. Just read right to left, and replace * with "pointer to". e.g. "const pointer to pointer to const int" for int const **const ptr6
You could have put in some declarations that actually require you to understand C declaration syntax:
int *(*(*x)(int (*)[4]))[];
Or you could trip people up with C++isms:
int *(*(Foo::*x)(int (Bar::**&)[5]) const &&)[];.
And as a little more of a challenge, write code to initialize those variables with something meaningful / make use of them.
You should be able to solve this.
it's a headache is what it is and the reason why so many languages today are C-like and not C
found the brainlet
Whoops my bad the first version indeed doesn't compile, this one should
const int(*(*a)(const float (*)[8]))[8]
You're close but not quite
Hint: it's just a single function pointer
Invalid declaration.
It would be a pointer to a function that takes a pointer to an array of 8 constant floats and returns an array of 8 constant ints, however it's not legal in C to return an array.
If you were to do const int (*(*a)(const float(*)[8]))[8] instead, that would be valid.
>const int(*(*a)(const float (*)[8]))[8]
a is a pointer to a function with one parameter (pointer to an array of 8 const floats) which returns a pointer to an array of 8 const ints.
Bingo
Babies second semester of c++.
It's all pointers
>int *(*(Foo::*x)(int (Bar::**&)[5]) const &&)[]
Ok here's my crack at that one:
declare x as a pointer to member function of Foo with parameter reference to pointer to pointer to member array of 5 ints of Bar returning const rvalue reference to array of pointers to int
that one does, yes.
that's a function pointer returning a pointer to a const int array of size 8, with the 1 argument being a const float array pointer of size 8
const int(*(*a)(const float(*)[8]))[8];
float my_cool_floats[8];
const int (*ints)[8] = a(&my_cool_floats);
is valid
did not clarify, the const int array of size 8 is a pointer to one
Just read them backwards.
template using ptr = T*;
#define EQ(x, ty) static_assert(std::is_same_v);
EQ(ptr1, ptr)
EQ(ptr2, const ptr)
EQ(ptr3, ptr)
EQ(ptr4, ptr)
EQ(ptr5, const ptr)
EQ(ptr6, const ptr)
EQ(ptr7, ptr)
EQ(ptr8, const ptr)
Not quite. The "const &&" means the member function being pointed to is a const member function, and the && is an rvalue ref-qualifier for the function. It means it points to a function something like:
class Foo {
void func() const &&;
};
The function actually returns a pointer to an array of unspecified bounds of pointers to ints.
Everything else you said is correct though.
>Assigning '0' to a pointer
Absolutely disgusting.
A pointer to a pointer is equivalent to a pointer to an array of pointers, and it is mostly used to pass arrays of strings as arguments to functions, other uses are some form of dynamic programming, using pointer to pointers of functions. Let's say that you want to support a class of hardware that does I/O, the hardware manufacturers provide the following standard interface.
int setup(); // setup the hardware return 0 on success, -1 otherwise
int read(int size, char[] buff); // read upto size chars into buff and return size read, -1 otherwise
int write(int size, char[] buff); // write the content of buff up to the sizeth element, return number of elements written or -1 if something went wrong
Now how do you go about loading a driver at runtime and cleanly looking up which driver to use when a given hardware is plugged in? One way to go about this (and I think it is how the linux kernel does it) is to provide an array of pointers (aka pointer to pointers)
struct IO_DRIVER_INTERFACE{
char ID[128]; // unique identifier of the hardware.
int (*setup)();
int (*read)(int, char[]);
int (*write)(int , char[]);
};
struct IO_DRIVER_INTERFACE *AVAILABLE_DRIVERS[256]; // we can support 256 such drivers
// loop over the above array and fill it with available drivers and assign NULLs to the rest of the array
// ... some LOC later
// the user installed a new driver and we got a new struct to add to our array
for(i=0; i
Can you give me a real world example of why someone would need to do this?
why?
Pointers to functions are useful. Passing pointers to arrays as arguments to functions isn't all that useful over just passing a pointer, but it allows you to keep the array as a fixed sized array which you can do `sizeof` on. Same story for returning a pointer to an array. To think of a concrete example of where you could use something like this, imagine a scenario where blocks of 8 numbers have some kind of significance, like they represent some coordinates of something. Now imagine you have some function which does something while making use of a transformation function which takes in these coordinates as doubles and outputs them as ints (discrete array indices maybe). This transformation function is customizable, and can be passed around via this function pointer. Unfortunately, because the values in the arrays are marked as const, it would be very hard to use these properly.
static const int (*transformer_func(const float (*vals)[8]))[8];
static void do_stuff(const int (*(*trans)(const float (*)[8]))[8]);
int main(void)
{
const int(*(*a)(const float (*)[8]))[8] = &transformer_func;
do_stuff(a);
return 0;
}
>using const
sizeof() doesn't work that way on array function parameters; you'll always just get the pointer size.
Not if you pass a pointer to an array. You can do sizeof *my_ptr_to_array.
void f(int (*ptr)[5])
{
size_t size_of_array = sizeof *ptr; /* 20 */
size_t num_of_elems = sizeof *ptr / sizeof **ptr; /* 5 */
}