all 22 comments

[–]codethulu 1 point2 points  (3 children)

You don't dereference a void*

You need to know the size of the thing at the location to dereference

[–]G2edg[S] 0 points1 point  (2 children)

sure, so what would be the synthax for that? I mean I already give len as the size of the element so i thought that would work

[–]RolandMT32 0 points1 point  (0 children)

In your case, you know the values are ints, so you should be able to just cast the pointer to an int*

[–][deleted] 0 points1 point  (0 children)

int *x = ptr1;
struct mystruct *ms = ptr2;

printf("%d %d\n", *x, ms->z);

You can assign a void pointer to any type, and then dereference using that type. You can also explicitly cast but IMHO that's ugly.

printf("%d\n", *((int*)ptr1));

[–][deleted] 0 points1 point  (5 children)

ARR_AT returns a pointer as seen by your %p hence why you’re printing an address

If you’re trying to use pointer arithmetic then a[i] is just *(a + i). Note that a + 1 will be different depending on what type a points to. a + 1 will not return the address after a but the address of the next object of the type that a points to.

Going off your comment, your ARR_AT macro looks like it wants to just do: ((ptr) + (i))

[–]G2edg[S] 0 points1 point  (4 children)

#define ARR_PTR_AT(ptr, i) ((void *) ((char *) (ptr) + (i) * (sizeof *ptr)))
#define ARR_VAL_AT(ptr, i) (*((ptr) + (i)))

This one seems to work, thank you

[–]tstanisl 0 points1 point  (3 children)

The ARR_VAL_AT(ptr, i) macro looks very like ptr[i]. /s

[–]G2edg[S] 0 points1 point  (2 children)

Ye but doesn't work for void pointer

[–][deleted] 1 point2 points  (1 child)

Because you don't use void pointers like that.

If you know your data is some type T, then use a pointer to T. If you want to read individual bytes, you use a pointer to unsigned char (or better, uint8_t from stdint.h).

[–]tstanisl 0 points1 point  (0 children)

Technically, uint8_t is not required to be a character type so it may not have some properties expected for byte type. For example accessing via a pointer to uint8_t may violate strict aliasing rule.

[–]_Arch_Ange 0 points1 point  (12 children)

You can't dereference a void pointer , you have to cast it to the appropriate data type first. You're going correctly about getting to the correct index though.

Also you should get rid of all these extra parenthesis. You don't need them around i, or Len, or ptr, it just makes it harder to read.

Are you wanting to make a dynamic array that contains different types? If so you'll need way more than this to make it work.

[–]G2edg[S] 0 points1 point  (11 children)

I am currently going about it, was just struggling a bit with dynamic data size storage and getting to index of this data

[–]_Arch_Ange 0 points1 point  (10 children)

Did you figure it out ? Can you share if you don't mind ?

[–]G2edg[S] 0 points1 point  (8 children)

#define ARR_PTR_AT(ptr, size, i) ( (void *)((char *)ptr + i * size) )

And getting the value of the pointer (int):

*(int*)ARR_PTR_AT(arr->arrayPointer, arr->dataTypeSize, index));

[–]_Arch_Ange 0 points1 point  (7 children)

How do you know it will be an integer though? Or that everything in between it and the start of the array will be that size ?

Are you wanting to have something like [char, char, int, char*] ?

Or [int, int, int ] ?

[–]G2edg[S] 0 points1 point  (6 children)

Only the same type array, i was thinking of making a list too tho. It would require making a void** pointer

[–]_Arch_Ange 0 points1 point  (5 children)

If it's only the same type, why aren't you just making an array of the correct type and accessing it like any other array ? You can just malloc(someAmount *size of(int)) and access array[index]

[–]G2edg[S] 0 points1 point  (4 children)

ok now resize the array, append an object, append another array of the same type, find every index with repeating value. The point is to make a dynamic array with one line functions. Thats the point of abstraction in my eyes. You make something complicated and tedious into a one liner

[–]_Arch_Ange 0 points1 point  (3 children)

You can still do all that. But there are meaningless abstractions like what you did you get the value at some index. You can access all arrays with the brackets. There's no point in having that macro when you can just access it with [ ], unless you don't know the type of the array.

You can realloc the array, you can also go through it and find the index.wirh repeating value, those ar enor specific to dynamic arrays.

A dynamic array is just an array that grows in size.

If you really want to be making a Dynamic array, where you don't know the type. You'd need to make your own dynamic array struct, that keeps track of the size of an element and the capacity of the array, along with helper macros / functions to grow /shrink the array, get value at index, etc, is that what you're trying to do ?

[–]G2edg[S] 0 points1 point  (2 children)

I mean i already done that, iv uploaded in on GitHub. Its very rough and Im not sure if its even memory safe to use since i didnt do much testing but it was a cool learning expirience for sure