I have a problem with my program. I am trying to run trough an array of pointers. Once the right pointer is found the program will print all the pointers that have been visited. I get error message at:
void * temp=debut[k];
ajouter(temp[k], resultat);
it says “void value not ignored as it ought to be”
I don’t uderstand why???
thank you in advance for help
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int calculerTaille(void * pointeur);
void ajouter(void * pointeur, char * resultat[]);
int verifier(void * debut, void * fin);
void * A[3];
void * D[3];
void * F[2];
void * G[4];
void * H[4];
void * J[3];
void * K[5];
void * L[4];
void * M[5];
int i = 0;
char * resultat[100];
int main(int argc, char * argv[]) {
A[0] = D;
A[1] = H;
A[2] = K;
D[0] = A;
D[1] = G;
D[2] = H;
F[0] = K;
F[1] = L;
G[0] = D;
G[1] = H;
G[2] = J;
G[3] = M;
H[0] = A;
H[1] = G;
H[2] = L;
H[3] = M;
J[0] = G;
J[1] = L;
J[2] = M;
K[0] = A;
K[1] = F;
K[2] = H;
K[3] = L;
K[4] = M;
L[0] = F;
L[1] = J;
L[2] = K;
L[3] = M;
M[0] = G;
M[1] = H;
M[2] = J;
M[3] = K;
M[4] = L;
void * debut = A;
void * fin = J;
ajouter(J, resultat);
while (verifier(debut, fin) != 1) {
srand(time(0));
int k = rand() % calculerTaille(K);
void * temp=debut[k]; //error
ajouter(temp[k], resultat); //error
}
int l=0;
for(l=0; resultat[l]!=NULL;l++) printf("%s ", resultat[l]);
return 0;
}
void ajouter(void * pointeur, char * resultat[]) {
if (pointeur == A)
resultat[i] = "A";
if (pointeur == D)
resultat[i] = "D";
if (pointeur == F)
resultat[i] = "F";
if (pointeur == G)
resultat[i] = "G";
if (pointeur == H)
resultat[i] = "H";
if (pointeur == J)
resultat[i] = "J";
if (pointeur == K)
resultat[i] = "K";
if (pointeur == L)
resultat[i] = "L";
if (pointeur == M)
resultat[i] = "M";
i++;
}
int verifier(void * debut, void * fin) {
if (debut == fin)
return 1;
else
return 0;
}
int calculerTaille(void * pointeur) {
if (pointeur == A)
return 3;
if (pointeur == D)
return 3;
if (pointeur == F)
return 2;
if (pointeur == G)
return 4;
if (pointeur == H)
return 4;
if (pointeur == J)
return 3;
if (pointeur == K)
return 5;
if (pointeur == L)
return 4;
if (pointeur == M)
return 5;
}
debutis a pointer of typevoid *. You can’t use the[]operator with a pointer of typevoid *. It is illegal in C. What is that supposed to mean in your code?You compiler, obviously, allows you to use the
[]withdebutas a compiler-specific extension (this is nevertheless illegal in C). However, the result ofdebut[k]is an “lvalue” of typevoid. You can’t read avoid. You can’t assign avoidto anything. That just doesn’t make any sense. What were you trying to say by attempting to read the “value” ofdebut[k]?Taking into account your comment, apparently what you need is
debutdeclared asNote two asterisks.
Again, if you want to access elements of the array
void * A[3]through an independent pointerdebut, the pointerdebuthas to be declared and initialized as followsThat way accessing
debut[k]will actually accessA[k].The same applies to
fin. In fact, your program contains numerous independent instances of this error (see function parameters as well), although some of them are more “forgiving” thandebut[k].P.S. Your cycle will loop infinitely, since you never change neither
debutnorfin.