I have a function which takes a struct pointers as parameter and returns it.ın the function i want another function fill up the memory pointerd by the pointer.my code is
struct my_struct{
unsigned char** ps;
unsigned long* pl;
};
struct* function(struct* param){
another_func(param->ps,param->pl)//function takes pointers as parameters and fills them up
return param;
}
int main{
my_struct *p;
p=function(p);
}
//definiton of another func is;
void another_func(unsigned char**,unsigned long * ){...}
EDIT:it gives the error access violation
From what you have posted so far try instead:
typedef struct my_struct
{
unsigned char** ps;
unsigned long* pl;
} my_struct;
void another_func(unsigned char**,unsigned long * ) {...}
my_struct* function(my_struct* param)
{
another_func(param->ps,param->pl)
return param;
}
int main()
{
my_struct *p;
my_struct q = {NULL,NULL};
unsigned long pl = 10;
q.ps = malloc( pl * sizeof(char*) );
q.pl = &pl;
p=function(&q);
return 0;
}
edited after chat
Related
typedef struct
{
void **heapArr;
int last;
int capacity;
int (*compare) (void *arg1, void *arg2);
} HEAP;
int heap_Insert( HEAP *heap, void *dataPtr);
I was doing my assignment of insert data to heap with abstract data type.
I allocated memory to heap, but I got problem of inserting data to heap.
I can't find out how to make void double pointer to char or int arrays.
void *x=dataPtr;
heap->(*heapArr)=&x;
I tried this way but I got failed. How can I make void double pointer to other type?
Why heapArr is a double pointer ? I think a single pointer is enought...
typedef struct
{
void *heapArr;
int last;
int capacity;
int (*compare) (void *arg1, void *arg2);
} HEAP;
int heap_Insert( HEAP *heap, void *dataPtr){
heap->heapArr = dataPtr;
return 0;
}
int main(void){
char str[] = "hello";
HEAP heapString;
heap_Insert(&heapString, str);
char * str_get = heapString.heapArr;
printf("%s\r\n", str_get );
int val = 101;
HEAP heapInt;
heap_Insert(&heapInt, &val);
int * int_get = heapInt.heapArr;
printf("%i\r\n", *int_get);
}
If you want to convert void* to char*, then following is the method -
(say void *data)
char *a = (char*) data;
Similarly for double pointer,
char **a = (char**) heapArr;
Simply typecast will do the conversion. Please try and comment back what you see.
I have a code with struct defination with member function pointers like this
struct file_system_type {
struct dentry *(*mount) (struct file_system_type *, int,
const char *, void *);
void (*kill_sb) (struct super_block *);
};
and object of file_system_type like this
static struct file_system_type minix_fs_type = {
.mount = minix_mount,
.kill_sb = kill_block_super,
};
and .mount like this
static struct dentry *minix_mount(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data)
I like to know what is the difference of above from function with return type some pointer like if I had something like this
static struct dentry* minix_mount(...)
struct dentry *(*mount) (struct file_system_type *, int,
const char *, void *);
void (*kill_sb) (struct super_block *);
are pointers a function that have return type struct dentry * resp. void. First you have to assign an actual function to these pointers to call these functions through these pointer. The pointers in your code are assigned with
.mount = minix_mount,
.kill_sb = kill_block_super,
static struct dentry *minix_mount(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data)
is a function that returns a pointer. It already has a static function body and can instantly be called.
Both calls return a value with the same type struct dentry *.
A big advantage of function pointers is that you can write generic code and assign a different functions to this pointer at run time. Common use-cases are algorithms like sort or find algorithms where you can pass a predicate or compare function to a function pointer.
Another advantage is that structs in C can contain function pointers but can't contain functions. That's a way to simulate OOP in C.
Here is an example of a function pointer to a function returning a pointer:
#include <stdio.h>
struct S {
int x;
int y;
// pointers to functions returning a pointer
int *(*compare1)(int *, int *);
int *(*compare2)(int *, int *);
};
// functions returning a pointer
int *min(int *a, int *b) {
return *a < *b ? a : b;
}
int *max(int *a, int *b) {
return *a > *b ? a : b;
}
int main() {
struct S s = {
.x = 5,
.y = 7,
.compare1 = &max,
// .compare2 = &min;
// name of a function can be used as function pointer
.compare2 = min
};
int *result1 = s.compare1(&s.x, &s.y);
int *result2 = s.compare2(&s.x, &s.y);
++*result1;
--*result2;
printf("%d %d", s.x, s.y);
}
Output:
4 8
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
union value {
long long i;
unsigned long long u;
double d;
long double ld;
void *p;
void (*g) ();
};
struct foo {
struct {
union value max;
union value min;
}limits;
};
struct bucket_info {
void *p; // free position
void *limit; // end position
struct bucket_info *next; // next bucket
};
#define NODES 8192
void * my_malloc(size_t size)
{
void *p = malloc(size);
if (!p)
exit(1);
memset(p, 0, size);
return p;
}
void * alloc_bucket(size_t size)
{
struct bucket_info *pb;
pb = my_malloc(sizeof(struct bucket_info) + size);
pb->p = pb + 1;
pb->limit = (char *)pb->p + size;
return pb;
}
void * alloc_for_size(struct bucket_info *s, size_t size)
{
void *ret;
while (s->next)
s = s->next;
if ((char *)s->p + size > (char *)s->limit) {
struct bucket_info *pb = alloc_bucket(size * NODES);
s->next = pb;
s = pb;
}
ret = s->p;
s->p = (char *)s->p + size;
return ret;
}
static void * alloc_node(struct bucket_info **s, size_t size)
{
if (!*s)
*s = alloc_bucket(size * NODES);
return alloc_for_size(*s, size);
}
static struct bucket_info *foo_info;
void * alloc_foo_node()
{
void *ret = alloc_node(&foo_info, sizeof(struct foo));
return ret;
}
struct foo * new_foo()
{
return alloc_foo_node();
}
void test(int t, struct foo *foo1)
{
struct foo *foo2 = new_foo();
// Crash at this line
*foo2 = *foo1;
// comment this switch statement, it works. why?
switch (t) {
case 1:
break;
default:
break;
}
}
int main(int argc, const char * argv[]) {
struct foo *foo1 = new_foo();
test(10, foo1);
return 0;
}
Above is the complete code. And I've compiled it with clang, got a 'Segment Fault 11' at line:
*foo2 = *foo1;
Then, change this line to:
memcpy(foo2, foo1, sizeof(struct Foo));
It works.
Then I've tried compiled these two cases with gcc, there is no problem.
The value returned by alloc_foo_node may not be correctly aligned for struct foo.
On my system, printing _Alignof(struct foo) gives 16, but the pointers foo1 and foo2 are not multiples of 16.
So it causes undefined behaviour to convert the unaligned result of alloc_foo_node to have type struct foo *.
To fix this you have to muck around a lot more with your allocation code, to make sure that it only ever hands out space that is on the correct boundary for struct foo. You could use max_align_t to help with this (it is defined so that _Alignof(max_align_t) is the biggest possible alignment required).
I have a struct defined as:
typedef struct {
int type;
void* info;
} Data;
and then i have several other structs that i want to assign to the void* using the following function:
Data* insert_data(int t, void* s)
{
Data * d = (Data*)malloc(sizeof(Data));
d->type = t;
d->info = s;
return d;
}
struct {
...
} Struct#;
then i just call
insert_data(1, variable_of_type_Struct#);
When i compile this it gives a warning
warning: assignment from incompatible pointer type
i tried to cast the variable in the insert to (void*) but didn't work
insert_data(1, (void *) variable_of_type_Struct#);
How can i get rid of this warning?
Thanks
Pass in the address of the struct, not a copy of it (i.e. not passed by value):
insert_data(1, &variable_of_type_Struct);
Pass a pointer to the struct object:
struct your_struct_type bla;
insert_data(1, &bla);
Hope this program helps!
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int type;
void* info;
} Data;
typedef struct {
int i;
char a;
float f;
double d;
}info;
Data* insert_data(int t, void* s)
{
Data * d = (Data*)malloc(sizeof(Data));
d->type = t;
d->info = s;
return d;
}
int main()
{
info in;
Data * d;
d = insert_data(10, &in);
return 0;
}
I'm not quite sure what this was:
struct {
...
} Struct#;
So, I cleaned up your program a little bit and got no warnings, after putting the address of the struct into the call, insert_data(1, &variable_of_type_Struct);
#include <stdlib.h>
#include <stdio.h>
typedef struct {
int type;
void* info;
} Data;
Data* insert_data(int t, void* s);
Data variable_of_type_Struct;
Data* insert_data(int t, void* s)
{
Data * d = (Data*)malloc(sizeof(Data));
d->type = t;
d->info = s;
return d;
}
void test()
{
insert_data(1, &variable_of_type_Struct);
}
insert_data waits for a void*, you put a Data.
insert_data(1, &variable_of_type_Struct#);
It miss a level of indirection.
Write a program to access the function "foo" using the structure structure2.
typedef struct
{
int *a;
char (*fptr)(char*);
}structure1;
typedef struct
{
int x;
structure1 *ptr;
}structure2;
char foo(char * c)
{
---
---
---
}
structure2 *s2 = (structure2*)malloc(sizeof(structure2));
s2->ptr = (structure1*)malloc(sizeof(structure1));
s2->ptr->fptr = foo;
char x = 'a';
s2->ptr->fptr(&x);
Create an object of type structure2
Assign to it the address of an object of type structure1 (this can be done in quite a few ways)
Assign foo to the above allocated structure1 object's fptr member
Call foo using:
structure2 s2;
// allocate
char c = 42;
s2.ptr->fptr(&c); // if this
Example:
typedef struct
{
int *a;
char (*fptr)(char*);
}structure1;
typedef struct
{
int x;
structure1 *ptr;
}structure2;
char foo(char * c)
{
return 'c';
}
int main()
{
structure1 s1;
structure2 s2;
s1.fptr = foo;
s2.ptr = &s1;
char c = 'c';
printf("%c\n", s2.ptr->fptr(&c));
return 0;
}