-2

I have to do a C project, but I never studied C. The compiler complains about a "Cast to pointer from integer of different size". Can you explain the nature of the problem and how I can fix it?

Here's my code :

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include <fcntl.h>
#include <time.h>
#include <stdint.h>
#include <pthread.h> //-lpthread commande

FILE* resultat=NULL; //déclaration d'un fichier nommé resultat qui contiendra les résultats issus du calcul des 3 fonctions

int T[199]; //déclaration d'un tableau de taille 199 en statique

static void *MARIT (void* i) //fonction qui calcul la moyenne arithmétique
{
    int s,j;
    int M1=0;

    //boucle qui calcul la moyenne arithmétique
    for (j=0;j<=150;j++)
    {
        s=(j*M1+T[j]);
        M1=s/(j+1);
        fprintf(resultat," Moyenne 1: %d \n",M1);//on écrit le résultat dans le fichier nommé resultat
    }

    return NULL;
}

static void *MQUAD (void* i) //fonction qui calcul la moyenne quadratique
{
    int s,j;
    int M2=0;

    //boucle qui calcul la moyenne quadratique
    for (j=0;j<=150;j++)
    {
        s=s+((T[j])*(T[j]));
        M2=sqrt(s/(j+1));
        fprintf(resultat," Moyenne 2: %d \n",M2);
    }

    return NULL;
    s=0;
}

static void *SCUB (void* i) //fonction qui calcul la somme des nombres cubique
{
    int s,j;
    int M3=0;

    //boucle qui calcul la somme des nombres cubique
    for (j=0;j<=150;j++)
    {
        s=M3+((T[j])*(T[j])*(T[j]));
        M3=s;
        fprintf(resultat," Moyenne 3: %d \n",M3);
    }

    return NULL;
}

int main (int argc, char* argv[]) {
    pthread_t tid1; //déclaration de 3 thread
    pthread_t tid2;
    pthread_t tid3;

    int M1,M2,M3;
    int f,i;
    int N=199;

    i=atoi(argv[1])-1; //remplace chaine de caractère par un entier (ici on demande à l'utilisateur d'entrer N)

    srand(time(NULL)); // initialisation de rand

    while (f<=N){ //Boucle qui permet de générer N nombres aléatoire stocké dans T
        T[f]= rand()%100; //N est demandé à l'utilisateur pour savoir combien il souhaite générer de nombres aléatoire
        printf("%i \n",T[f]);
        f++;
    }

    resultat=fopen("Resultatthread1.txt","w+"); //Création du fichier Resultat

    //creation des thread, 3 thread pour 3 fonctions
    pthread_create(&tid1,NULL,MARIT,(void *)i);
    pthread_create(&tid2,NULL,MQUAD,(void *)i);
    pthread_create(&tid3,NULL,SCUB,(void *)i);

    //Ici on attend la fin d'execution des 3 thread pour pouvoir terminer le programme
    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
    pthread_join(tid3,NULL);

    return 0;
    fclose(resultat);
}
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
tinmar
  • 1
  • 2
    you have a weird fetish for empty lines. – Karoly Horvath Nov 24 '14 at 16:50
  • `i` is an `int`, not an `int*`, and since it's being given a value from `argv` is it meant to be a `char*`? – IllusiveBrian Nov 24 '14 at 16:52
  • none of your functions ever uses the `i` parameter. Just pass `0` instead of `(void*)i` and see if the error persists. In any case, how are we supposed to guess which line produced the error? Did you try to narrow the location of the error down? – Jens Gustedt Nov 24 '14 at 17:08
  • Hi, sorry, the error is on line "pthread_create(&tid1,NULL,MARIT,(void *)i);", i forgot say it! i'm gonna try with "0" and see what happends, ty for your help – tinmar Nov 24 '14 at 17:24
  • When i'm trying with "0" i get a segmentation fault "core dumped" – tinmar Nov 24 '14 at 17:28

1 Answers1

0

I assume this is where the error is

pthread_create(&tid1,NULL,MARIT,(void *)i);

the last parameter is meant to be a pointer to an arbitrary block of thread data. You can't cast an arbitrary int like that. YOu are assuming that an int is the same size as a pointer.

pm100
  • 48,078
  • 23
  • 82
  • 145