【问题标题】:string pointers not working between functions字符串指针在函数之间不起作用
【发布时间】:2015-02-24 17:11:47
【问题描述】:

我试图通过在函数之间传递一个指向字符串的指针,在每种情况下将不同的错误消息分配给错误变量,但由于任何原因都不能正常工作。

代码如下:

//This function takes a pointer to a char.
int lee(Fecha *f, char *error){ 
    int checking;
    printf("Introduzca una fecha compuesta por día, mes, y año\n");
    checking = scanf("%i %i %i", &f->day, &f->month, &f->year);
    printf("%d\n", checking);
    switch (checking){
        case 0:
            //The message is assigned to the space where error points to.
            *error = "Formato de fecha incorrecto. El día debe ser un entero."; 
        break;
        case 1:
            *error = "Formato de fecha incorrecto. El mes debe ser un entero.";
        break;
        case 2:
            *error = "Formato de fecha incorrecto. El año debe ser un entero.";
        break;
    }
    return (checking == 3);
}

int main(){
    Fecha f;
    //error is declared like a pointer
    char * error; 
    int ret;
    //The pointer is passed to the function as argument.
    ret = lee(&f, error); 
    printf("%s", error);
    return 0;
}

还有输出:

user@console:~/$ ./Sesion1 
Introduzca una fecha compuesta por día, mes, y año (o 0 0 0 para terminar el programa)
23 dfgadkfhgsñdgh 5
1
Segmentation fault

【问题讨论】:

  • 看看*error 并思考这意味着什么。你给它分配了什么?它是如何记忆的?
  • 是的.. 寻找 malloc 的错误.... 失败....

标签: c function pointers arguments


【解决方案1】:

由于要对字符串进行输出,因此需要将指针传递给 char*,因此需要将 error 作为 char** 传递。

所以函数会是

int lee(Fecha *f, char **error){ 
    int checking;
    printf("Introduzca una fecha compuesta por día, mes, y año\n");
    checking = scanf("%i %i %i", &f->day, &f->month, &f->year);
    printf("%d\n", checking);
    switch (checking){
        case 0:
            //The message is assigned to the space where error points to.
            *error = "Formato de fecha incorrecto. El día debe ser un entero."; 
        break;
        case 1:
            *error = "Formato de fecha incorrecto. El mes debe ser un entero.";
        break;
        case 2:
            *error = "Formato de fecha incorrecto. El año debe ser un entero.";
        break;
    }
    return (checking == 3);
}

int main(){
    Fecha f;
    //error is declared like a pointer
    char * error; 
    int ret;
    //The pointer is passed to the function as argument.
    ret = lee(&f, &error); 
    printf("%s", error);
    return 0;
}

此外,您应该考虑不要在声明后留下没有值的指针,就像在 main() 中处理 char* 错误一样。

问题是,当您有一个未初始化的指针然后您忘记给它一个值或类似的东西时,它可以指向您的应用程序内存中的任何地方(或超出它)。虚拟内存可以保护您免于读取和写入其他应用程序的内存(一般而言),但是您可能会遇到指针恰好指向应用程序内部的某些数据的情况。如果你再用这个指针来写或读,你会得到不可预知的结果,从只是显示错误的数据到损坏的数据或崩溃的应用程序,这是你显然不想要的。

【讨论】:

  • 感谢您的回复(实际上感谢所有人)。也许这种不可预知结果的情况可以解决初始化错误指针为NULL? char * error = NULL;
  • 没问题,每个人都需要从某个地方开始。在继续之前,请确保您通过指针和内存了解所有这些内容。
【解决方案2】:

你需要传递一个指向你的函数输出参数的指针。由于您的输出参数是char*,指向它的指针是char**

要修复,请将相应的行更改为:

int lee(Fecha *f, char **error);
// ...
ret = lee(&f, &error);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 2010-11-08
    • 2015-12-09
    • 2012-12-11
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    相关资源
    最近更新 更多