【问题标题】:pointers not read correctly指针未正确读取
【发布时间】:2015-10-20 16:22:18
【问题描述】:

我正在尝试从数组中获取输入、输出和数据文件的名称以进行进一步处理。但是,我遇到了一个奇怪的错误或问题。所以,我的程序没有到达 for 循环。它甚至不打印 for 循环之前的语句。但是,我尝试使用调试器并且程序正在逐步正确打印。因此,当我运行它时它不会打印,而当我逐步调试时它会打印。那很奇怪!

char *method;
        method=malloc(25);
        method=NULL;
        char *dataFileName;
        char *inputMethod;
        inputMethod=malloc(25);
        inputMethod=NULL;
        char *inputFileName;
        char *outputMethod;
        outputMethod=malloc(25);
        outputMethod=NULL;
        char *outputFileName;
        char *commandArray[]={"if=q.txt","of=output.txt"};
        char**args=(char**) malloc(sizeof(char*)*256);
        args=commandArray;
        int i;
        printf("Before second for");
        for(i=0;i<2;i++)
       {
            printf("I am here");
            if(*args[i]=='d')
                {
                    method=strtok_r(args[i],"=",&dataFileName);
                    printf("The method given is %s",method);
                    printf("Data File Name is %s",dataFileName);
                }
                else if(*args[i]=='o')
                {
                    outputMethod=strtok_r(args[i],"=",&outputFileName);
                    printf("The output method given is %s",outputMethod);
                    printf("output File Name is %s",outputFileName);
                }
                else
                {
                    inputMethod=strtok_r(args[i],"=",&inputFileName);
                    printf("The input method given is %s",inputMethod);
                    printf("Input File Name is %s",inputFileName);
                }
            }

        if(method==NULL)
        {
                dataFileName=malloc(256);
                printf("Please Enter A File Name");
                scanf("%255s",dataFileName);
                printf("%s",dataFileName);
        }

        if((inputMethod==NULL)||(outputMethod==NULL) )
        { 
            char* array[]={"stdin","stdout"};
            if(inputMethod==NULL)
                inputMethod=array[0];
            if(outputMethod==NULL)
                outputMethod=array[1];
        }

我正在使用 C 语言中的 Netbeans 进行开发。上面的代码是在 main 中编写的。谢谢!

【问题讨论】:

  • 请从阅读C书开始。您的“代码”中有很多基本错误,表明对 C 中的基本概念缺乏理解。
  • 你想做什么?第一步是为指针分配内存,在下一行中让它们指向NULL,然后再次分配内存。
  • 我知道我尝试了很多调整以使这个东西以某种方式运行,但仍然无法弄清楚。这就是原因。

标签: c arrays pointers printf


【解决方案1】:

我故意留下了前面的答案,因为理解内存分配在 c 编程中是微不足道的。我看到你有一个大问题。

但你几乎在每件事上都有问题。在我的实际答案中,我将尝试简化您如何使用 strtok,拆分字符串并解析它。我想这是您的代码的第二个主要问题。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main(void){
    char commandArray[][256]={
        "if=q.txt",
        "of=output.txt"
    };

    char infile[256], outfile[256];

    for(int i=0; i<2;i++){

        char *ptr,*cmd;

        cmd=commandArray[i];
        ptr=NULL;

        printf("parsing command '%s'\n",cmd);

        cmd=strtok(cmd,"=");
        ptr=strtok(NULL,"=");

        if(!cmd){
            printf("Error parsing the string '%s'\n",commandArray[i]);
            exit(1);
        }

        if (strcmp(cmd,"if")==0){
            strcpy(infile,ptr);
        }
        else if (strcmp(cmd,"of")==0){
            strcpy(outfile,ptr);
        }
        else{
            printf("unknow token '%s'\n",cmd);
            exit(1);
        }
    }


    printf(
        "\n\n"
        "input file: '%s'\n"
        "output file: '%s'\n"
        "\n\n",
        infile,outfile);

    return 0;
 }

【讨论】:

  • 谢谢!是的,当我在分配之前分配内存时,问题就解决了!
【解决方案2】:

主要问题是这样的:

char *method;
method=malloc(25);//allocating space for 25 char
method=NULL; // throwing up the allocation without freeing it;
             // now the allocation is lost
             // now method is useless (it is null)

【讨论】:

  • 这是一个大问题,但我不确定这是 主要 问题。还有很多其他大问题。
  • 我没有分析代码。那个指针刚刚跳到我的眼前。
猜你喜欢
  • 1970-01-01
  • 2018-06-10
  • 2019-11-01
  • 2012-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-26
  • 1970-01-01
相关资源
最近更新 更多