pointer inside struct to dynamic 2-D array - c

Hope you can help me since I'm a little desperate right now. Im working with threads and I want them all to modify the same 2D array. So I made a struct with a pointer to the 2D array, but it isn't working. This is my code
typedef struct {
char XY;
int b;
int d;
int f;
int h;
} celdaMatriz;
typedef struct {
int totalX;
int totalY;
celdaMatriz **matriz;
char direction;
int i;
int j;
int cont;
} parameter;
As you can see parameter has the variable " celdaMatriz matriz**" (I'm sorry about the spanish in my code). Now comes a function to initialize a "celdaMatriz" 2D array
void createMatriz(int totalX, int totalY, celdaMatriz **matriz[totalX][totalY])
{
int i,j;
for(i=0;i<totalX; i++){
for(j=0; j<totalX;j++){
matriz[i][j]->XY=' ';
matriz[i][j]->b=0;
matriz[i][j]->d=0;
matriz[i][j]->f=0;
matriz[i][j]->h=0;
}
}
matriz[0][3]->XY='/';
matriz[2][0]->XY='*';
matriz[2][1]->XY='*';
matriz[0][2]->XY='*';
matriz[2][2]->XY='*';
matriz[1][3]->XY='*';
matriz[3][3]->XY='*';
}
the function createMatriz will change, but that's the idea. So now my main function
int main( int argc,char *argv[])
{
int i,j;
celdaMatriz **matriz = malloc( sizeof (*matriz) * 4);
for (i=0; i < 4; ++i)
matriz[i] = malloc(sizeof (**matriz) * 4);
crateMatriz(4,4,matriz);
parameter *p1,p2;
parameter *p1 = malloc(sizeof(*p1));
p1->totalX=4;
p1->totalY=4;
p1->i=0;
p1->j=0;
p1->cont=0;
p1->direction= 'f';
p1->matriz= matriz;
return 0;
}
So anytime I want to create a new thread I create a new parameter variable and its "matriz" variable is a pointer to the 2D array I created in the main function. Well at least that's the idea, but I get these errors
**request for member 'XY' in something not a structure or union**
**request for member 'b' in something not a structure or union**
**request for member 'd' in something not a structure or union**
**request for member 'f' in something not a structure or union**
**request for member 'h' in something not a structure or union**
passing argument 3 of 'crearMatriz' from incompatible pointer type [enabled by default]
main.c: 59:6: note: expected 'struct celdaMatriz ** (*) [(unsigned int) (totaly)]' but argument is of type 'struct celdaMatriz **
The errors come from the createMatrix function and the last one from the main function. By the way I'm using linux and C.
Thanks. Hope you can help me.
EDITED
I just fixed an error, this
*passing argument 3 of 'crearMatriz' from incompatible pointer type [enabled by default]
main.c: 59:6: note: expected 'struct celdaMatriz * () [(unsigned int) (totaly)]' but argument is of type 'struct celdaMatriz *
is already fixed. So the problem now is that I'm accessing matriz[i][j]->XY=' '; incorrectly
Hope you can help me since I'm a little desperate right now. Im working with threads and I want them all to modify the same 2D array. So I made a struct with a pointer to the 2D array, but it isn't working. This is my code
typedef struct {
char XY;
int b;
int d;
int f;
int h;
} celdaMatriz;
typedef struct {
int totalX;
int totalY;
celdaMatriz **matriz;
char direction;
int i;
int j;
int cont;
} parameter;
As you can see parameter has the variable " celdaMatriz matriz**" (I'm sorry about the spanish in my code). Now comes a function to initialize a "celdaMatriz" 2D array
void createMatriz(int totalX, int totalY, celdaMatriz **matriz[totalX][totalY])
{
int i,j;
for(i=0;i<totalX; i++){
for(j=0; j<totalX;j++){
matriz[i][j]->XY=' ';
matriz[i][j]->b=0;
matriz[i][j]->d=0;
matriz[i][j]->f=0;
matriz[i][j]->h=0;
}
}
matriz[0][3]->XY='/';
matriz[2][0]->XY='*';
matriz[2][1]->XY='*';
matriz[0][2]->XY='*';
matriz[2][2]->XY='*';
matriz[1][3]->XY='*';
matriz[3][3]->XY='*';
}
the function createMatriz will change, but that's the idea. So now my main function
int main( int argc,char *argv[])
{
int i,j;
celdaMatriz **matriz = malloc( sizeof (*matriz) * 4);
for (i=0; i < 4; ++i)
matriz[i] = malloc(sizeof (**matriz) * 4);
crateMatriz(4,4,matriz);
parameter *p1,p2;
parameter *p1 = malloc(sizeof(*p1));
p1->totalX=4;
p1->totalY=4;
p1->i=0;
p1->j=0;
p1->cont=0;
p1->direction= 'f';
p1->matriz= matriz;
return 0;
}
So anytime I want to create a new thread I create a new parameter variable and its "matriz" variable is a pointer to the 2D array I created in the main function. Well at least that's the idea, but I get these errors
**request for member 'XY' in something not a structure or union**
**request for member 'b' in something not a structure or union**
**request for member 'd' in something not a structure or union**
**request for member 'f' in something not a structure or union**
**request for member 'h' in something not a structure or union**
passing argument 3 of 'crearMatriz' from incompatible pointer type [enabled by default]
main.c: 59:6: note: expected 'struct celdaMatriz ** (*) [(unsigned int) (totaly)]' but argument is of type 'struct celdaMatriz **
The errors come from the createMatrix function and the last one from the main function. By the way I'm using linux and C.
Thanks. Hope you can help me.
EDIT
I just fixed an error, this
*passing argument 3 of 'crearMatriz' from incompatible pointer type [enabled by default]
main.c: 59:6: note: expected 'struct celdaMatriz * () [(unsigned int) (totaly)]' but argument is of type 'struct celdaMatriz *
is already fixed. So the problem now is that I'm accessing matriz[i][j]->XY=' '; incorrectly
EDIT
Hi. I just want to let you know that I already found an answer to my problem. Instead of send a pointer to the matrix, I will use it as a global variable, that way all the threads will be able to modify it. So the solution is 1- Declare a global celdaMatriz **matriz
celdaMatriz **matriz;
Next I changed the memory allocation
matriz = (celdaMatriz **) malloc(4 * sizeof(celdaMatriz *));
for (i = 0; i < 4; ++i)
{
matriz[i] = (celdaMatriz *) malloc(4 * sizeof(celdaMatriz));
}
Finally the new void createMatriz function:
void crearMatriz(int totalX, int totalY)
{
int i=0,j=0;
for(i=0; i<totalX;i++){
for(j=0; j<totalY; j++){
celdaMatriz *nodo = malloc(sizeof(celdaMatriz));
nodo->b=0;
nodo->d=0;
nodo->f=0;
nodo->h=0;
nodo->casilla=0;
matriz[i+j*totalY]=(celdaMatriz *)nodo;
}
}
matriz[12]->casilla=1;
for(i=0; i<totalX; i++){
for(j=0; j<totalY; j++){
printf(" %d ", matriz[i+j*totalY]->casilla);
}
printf("\n");
}
}
And that's it. Thanks to all of you who helped me with this problem

I guess that the problem is that you tried to call creatMatriz like this,
createMatriz(p1->totalX, p1->totalY, p1->matriz);
and C (C99) does not like it. The problem is that p1->matriz is of type struct celdaMatriz **, but your prototype of createMatriz asked for a struct * (*) [totalY] because C ignores [totalX].
If you want to keep the definition of parameter unchanged, I would suggest you just use a double pointer.

Related

Setting array of pointers

i'm doing a small exercise to load an array of pointers (double pointer) to a struct. I have the following definition in the header file:
#include <stdio.h>
#define LEN (5)
typedef struct sample_s {
int num;
char *name;
}sample_t;
typedef struct new_sample_s {
char *string;
sample_t **sample_arr;
}new_sample_t;
sample_t table[LEN] = {
{0, "eel"},
{1, "salmon"},
{2, "cod"},
{3, "tuna"},
{4, "catfish"}
};
and using the definitions int this .c file:
#include "test.h"
void print_new_sample_array(sample_t **sample_arr) {
int len = sizeof(table)/sizeof(new_sample_t);
for(int i = 0; i < len; i++){
printf("The array element is: %s\n", sample_arr[i]->name);
}
}
int main() {
new_sample_t new_sample;
new_sample.sample_arr = table;
print_new_sample_array(new_sample.sample_arr);
return 0;
}
I have two questions:
First I'm not sure how to correctly load the table to the new_sample.sample_arr
Error message here:
test.c: In function ‘main’:
test.c:13:27: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
new_sample.sample_arr = table;
^
Second, I'm not sure how I can refer to the properties of each element in the sample_arr. For example, when I do the following, the program errored out:
for(int i = 0; i < LEN; i++){
printf("This is the elem in the array: %s", new_sample[i]->name);
}
I'm trying to learn more about the double pointer concept and why I did it wrong. I would really appreciate the answer keeps the sample_arr as double pointer
Thank you!
In this assignment statement
new_sample.sample_arr = table;
the right operand (after implicit conversion of the array to pointer to its first element) has the type sample_t * while the left operand has the type sample_t ** due to the declaration of the data member
sample_t **sample_arr;
There is no implicit conversion from the type sample_t * to the type sample_t **. So the compiler issued a message.
You should declare the data member like
sample_t *sample_arr;
and correspondingly the function declaration will look like
void print_new_sample_array(sample_t *sample_arr);
And within the function the call of printf will look like
printf("The array element is: %s\n", sample_arr[i].name);

C program: need help using pointers

The purpose of this program is to assign the names to their corresponding ages using pointers.
#include <stdio.h>
#include <stdlib.h>
/* these arrays are just used to give the parameters to 'insert',
to create the 'people' array
*/
#define HOW_MANY 7
char *names[HOW_MANY]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim",
"Harriet"};
int ages[HOW_MANY]= {22, 24, 106, 6, 18, 32, 24};
/* declare your struct for a person here */
typedef struct {
char *name;
int age;
} person;
static void insert(person *people[], char *name, int age, int *nextfreeplace)
{
/* creates memory for struct and points the array element to it. */
people[*nextfreeplace] = malloc(sizeof(person));
/* put name and age into the next free place in the array parameter here */
(*people[*nextfreeplace]).name = name;
(*people[*nextfreeplace]).age = age;
/* modify nextfreeplace here */
(*nextfreeplace)++;
}
int main(int argc, char **argv)
{
/* declare the people array here */
person *people[HOW_MANY];
int nextfreeplace = 0;
for (int i = 0; i < HOW_MANY; i++)
{
insert (&people, names[i], ages[i], &nextfreeplace);
}
/* print the people array here*/
for (int i = 0; i < HOW_MANY; i++) {
printf("Name: %s. Age: %d\n", (*people[i]).name, (*people[i]).age);
}
/* Releases the memory allocated by malloc */
for (int i = 0; i < HOW_MANY; i++) {
free(people[i]);
}
return 0;
}
It works perfectly, but when I compile it I get two warnings.
arrays.c: In function ‘main’:
arrays.c:41:13: warning: passing argument 1 of ‘insert’ from incompatible pointer type [-Wincompatible-pointer-types]
insert (&people, names[i], ages[i], &nextfreeplace);
^
arrays.c:19:13: note: expected ‘person ** {aka struct <anonymous> **}’ but argument is of type ‘person * (*)[7] {aka struct <anonymous> * (*)[7]}’
static void insert(person *people[], char *name, int age, int *nextfreeplace)
I'm new to pointers and C in general and would like some help explaining why I get these warnings and how to get rid of them. Thanks!
TL; DR
Use people instead of &people.
Long explanation
Here is what the warning messages say:
Your function insert expects a parameter of type person ** (a pointer to pointer to person). Your code sends it a parameter of different type: person * (*)[7], which is a C way for "a pointer to an array of 7 pointers to person".
(you can use the site http://cdecl.org to discover that: enter struct person * (*people)[7] in its field, and it will translate it to English)
If you send your array, and not a pointer to it, to your insert function, the compiler will regard the name people as a "pointer to pointer to person", which in this context is a special case of "array of pointers to person". This process is called "decay" of array to a pointer, and is explained here.
You can also do this:
/* declare your struct for a person here */
typedef struct {
char *name;
int age;
} person, *people_t;
And then
static void insert(people_t people[], char *name, int age, int *nextfreeplace){...}
Then
people_t people[HOW_MANY];
int nextfreeplace = 0;
for (i = 0; i < HOW_MANY; i++){
insert (people, names[i], ages[i], &nextfreeplace);
}
Compiler: Visual Studio 2010
This is actually a single warning, shown over two lines (the source of the warning at line 41, and the declaration causing the problem at line 19).
You can clear the warning by removing the ampersand from the call to insert, thus
insert(people, names[i], ages[i], &nextfreeplace);
In C, the name of an array is synonymous with its address.
Also, to clear up the <anonymous> tag in the warnings, a common idiom when typedefing structures is the following:
typedef struct <name>
{
...
} <name>;
which in your case would be:
typedef struct person {
char *name;
int age;
} person;
You get these warning because the "insert" expects pointer-to-pointer-to-person, but gets just a pointer-to-pointer-to-pointer-to-person.
In line 41, get rid of "&" from "&people", like in:
insert (people, names[i], ages[i], &nextfreeplace);

Function pointer confusion

I'm trying to use function pointers and Abstract data types in c. This is my first time using it and I'm really confused. Anyways when I tried to compile this code I gave me an error. The first time I ran it worked. But when I change the arguments by switch a and b. It gave me the old answer and never updated.
typedef struct data_{
void *data;
struct data_ *next;
}data;
typedef struct buckets_{
void *key;
}buckets;
typedef struct hash_table_ {
/* structure definition goes here */
int (*hash_func)(char *);
int (*comp_func)(void*, void*);
buckets **buckets_array;
} hash_table, *Phash_table;
main(){
Phash_table table_p;
char word[20]= "Hellooooo";
int a;
a = 5;
int b;
b = 10;
/*Line 11*/
table_p = new_hash(15, *print_test(word), *comp_func(&a, &b));
}
int *print_test(char *word){
printf("%s", word);
}
int *comp_func(int *i,int *j){
if(*i < *j){
printf("yeeeeeeee");
}else{
printf("yeaaaaaaaaaaaaa");
}
}
Phash_table new_hash(int size, int (*hash_func)(char *), int (*cmp_func)(void *, void *)){
int i;
Phash_table table_p;
buckets *buckets_p;
hash_table hash_table;
table_p = (void *)malloc(sizeof(hash_table));
/*Setting function pointers*/
table_p -> hash_func = hash_func;
table_p -> comp_func = cmp_func;
/*Create buckets array and set to null*/
table_p -> buckets_array = (buckets **)malloc(sizeof(buckets_p)*(size+1));
for(i = 0; i < size; i++){
table_p -> buckets_array[i] = NULL;
}
return table_p;
}
This is the error message:
functions.c: In function 'main':
functions.c:11:26: error: invalid type argument of unary '*' (have 'int')
functions.c:11:45: error: invalid type argument of unary '*' (have 'int')
Helloyeaaaaaaaaaaaaa
New error:
functions.c:11:3: warning: passing argument 2 of 'new_hash' makes pointer from integer without a cast [enabled by default]
hash.h:29:13: note: expected 'int (*)(char *)' but argument is of type 'int'
functions.c:11:3: warning: passing argument 3 of 'new_hash' makes pointer from integer without a cast [enabled by default]
hash.h:29:13: note: expected 'int (*)(void *, void *)' but argument is of type 'int'
If you want to pass a function as a function-pointer, simply provide the name:
new_hash(15, print_test, comp_func);
or alternatively (and equivalently), use the & symbol to make it clear what's going on:
new_hash(15, &print_test, &comp_func);
You should declare function before using it. If you don't do this, the compiler assumes that it returns int, and gives you an error when you try to dereference it (since it's impossibole to dereference int).
EDIT:
you may also misunderstood the concept of function pointers. you should not pass the result of print_test(word) to new_hash - you should pass print_test itself. (also, change its return type)

warning: initialisation from incompatible pointer type

Hi all it's been sometime since I've touched C so I'm really rusty on it. I wrote a small program to create a matrix using two dynamic arrays. However, I'm getting this warning and I don't understand why? I guess I'm not quite sure about pointers to pointers. Can someone help me point out where my problem is? Thanks.
sm.c: In function ‘main’:
sm.c:11:13: warning: initialisation from incompatible pointer type [enabled by default]
sm.c: In function ‘makeMatrix’:
sm.c:27:3: warning: return from incompatible pointer type [enabled by default]
#include <stdio.h>
#include <stdlib.h>
typedef int (**intptr) ();
intptr makeMatrix(int n);
int main(int argc, char *argv[]) {
int n = 2;
int **A = makeMatrix(n);
if(A) {
printf("A\n");
}
else printf("ERROR");
}
intptr makeMatrix(int size) {
int **a = malloc(sizeof *a * size);
if (a)
{
for (int i = 0; i < size; i++)
{
a[i] = malloc(sizeof *a[i] * size);
}
}
return a;
}
You've got some problems here:
typedef int (**intptr) ();
intptr makeMatrix(int n);
...
int **A = makeMatrix(n);
The intptr typedef declares a pointer to a pointer to a function that takes an indeterminate number of arguments and returns an int. A is not an int.
You need to write:
int **makeMatrix(int n);
int **A = makeMatrix(n);
Using a typedef won't help much here.
typedef int **(*intptr)();
That declares a pointer to a function that returns a pointer to a pointer to an int. But writing
intptr makeMatrix(int n);
would declare that makeMatrix() returns a pointer to a function, not an int **.
Your typedef has an extra (), making it a zero argument function type. Remove that and you should be good.

How do I call an arbitrary C function passed to another function?

I'm writing a unit test framework (see SO for more details). Or view the code at GitHub.
Safer Code describes a way to pass functions of arbitrary types.
But how do I call such a function without knowing its types beforehand? Assume f needs no input, so f() should work on its own.
Let's say I want to populate an array using an arbitrary generator function.
void* gen_array(fp gen, size_t size) {
int i, len = gen_int() % 100;
void* arr = GC_MALLOC(len * size);
for (i = 0; i < len; i++) {
arr[i] = gen(NULL);
}
return arr;
}
It should look something like this, but I get compiler errors:
gcc -o example example.c qc.c qc.h -lgc
In file included from example.c:1:
qc.h:21: error: expected declaration specifiers or ‘...’ before ‘size_t’
In file included from qc.c:1:
qc.h:21: error: expected declaration specifiers or ‘...’ before ‘size_t’
qc.c:23: error: conflicting types for ‘gen_array’
qc.h:21: error: previous declaration of ‘gen_array’ was here
qc.c: In function ‘gen_array’:
qc.c:29: warning: dereferencing ‘void *’ pointer
qc.c:29: error: too many arguments to function ‘gen’
qc.c:29: error: invalid use of void expression
qc.h:21: error: expected declaration specifiers or ‘...’ before ‘size_t’
make: *** [example] Error 1
After thinking about some more I realize your problem your above code would never work.
You are first calling trying to call a void function with no parameters with the parameter NULL. Next you would need your code to be more generic. I placed an example below of what I mean. Now using a global variable
#include <stdio.h>
#include <stdlib.h>
typedef void (*fp)(void);
void * GEN_ARRAY_TEMP;
int gen_int() {
return 67;
}
void* gen_array(fp gen, size_t size) {
int i, len = gen_int() % 100;
void* arr = malloc(len * size);
void* arr_end = arr + len * size;
GEN_ARRAY_TEMP = arr;
while (GEN_ARRAY_TEMP <= arr_end) {
gen();
GEN_ARRAY_TEMP+=size;
}
return arr;
}
void make_int() {
(*(int*)GEN_ARRAY_TEMP) = 9;
}
int main() {
int i;
int * gen_int_array = (int*) gen_array(make_int, sizeof(int));
for(i=0;i<67;i++) {
printf("%d\n",gen_int_array[i]);
}
}
That page suggests you make the function pointer take a void*. So in order for your code to compile, you must pass it a void pointer:
typedef void* (*fp)(void*);
doit(fp f) {
f(NULL);
}
And just make sure that the function that you're calling simply ignores the parameter.
Generally speaking, these generic function pointers are used for starting threads. The void pointer is simply a pointer to a struct that holds the actual parameters.
What would you need to do is wrap your function in a void function like so
#include <stdio.h>
typedef void (*fp)(void);
int sum(int x,int y) {return x+y;}
void doit(fp f) {
f();
}
void func() {
printf("Hello %d\n",sum(1,2));
}
int main() {
doit(func);
}
You have two problems:
First, qc.h is missing a <stdlib.h> include. This is needed for use of size_t.
Second, in gen_array, you create a void *arr, then try to dereference it as an array (arr[i]). Since the compiler doesn't know the size of your array elements, it cannot fill the array. You must treat it as a char *, offset by arr + size * i, and pass it into gen rather than taking a return (returns also need to know the structure size):
// ...
char *arr = GC_MALLOC(len * size);
for (int i = 0; i < len; i++) {
gen(arr + i * size, NULL);
}
return arr;
This will of course require changing the fp type definition.
For the case where your pointer to a function 'fp' is of type which takes no argument and returns void, in which case you should declare it as :
typedef void (*fp)();
In the above case the call should be :
(*gen)();
If your pointer to the function 'fp' is of type which takes 'void *' as argument and returns void, in which case you should declare it as :
typedef void (*fp)(void *);
In the above case the call should be :
(*gen)(NULL);
or any other pointer variable you might want to pass.
As far as your example goes try this :
typedef void * (*fp)(void *);
void* gen_array(fp gen, size_t size) {
int i, len = gen_int() % 100;
void* arr = GC_MALLOC(len * size);
for (i = 0; i < len; i++) {
arr[i] = (*gen)(NULL);
}
return arr;
}

Resources