【问题标题】:Cast to pointer from integer of different size? [duplicate]从不同大小的整数转换为指针? [复制]
【发布时间】:2014-11-24 16:48:39
【问题描述】:

我必须做一个 C 项目,但我从未学习过 C。编译器抱怨“从不同大小的整数转换为指针”。您能解释一下问题的性质以及如何解决吗?

这是我的代码:

#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);
}

【问题讨论】:

  • 你对空行有一种奇怪的迷恋。
  • i 是一个int,而不是int*,并且由于它是从argv 获得的值,所以它是一个char*
  • 您的任何函数都没有使用过i 参数。只需通过 0 而不是 (void*)i 并查看错误是否仍然存在。无论如何,我们应该如何猜测哪一行产生了错误?您是否尝试缩小错误的位置?
  • 嗨,对不起,错误是在线“pthread_create(&tid1,NULL,MARIT,(void *)i);”,我忘了说!我将尝试使用“0”,看看会发生什么,请您帮忙
  • 当我尝试使用“0”时,我得到一个分段错误“核心转储”

标签: c


【解决方案1】:

我认为这是错误所在

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

最后一个参数是指向任意线程数据块的指针。你不能像那样投射任意的 int 。您假设 int 与指针的大小相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 2014-02-14
    相关资源
    最近更新 更多