小编Mad*_*Sun的帖子

在C中使用getchar(); 每次使用它都会移动到下一个字符吗?包括在任务操作中?

我在用C语言编写程序时使用getchar()(在课程的这一点上还不允许使用scanf.)我想知道如果它移动到下一个,我是否每次调用它; 包括在任务操作期间.例如; 我试图从控制台读取双倍; 并确定前面是否有负号.如果是的话; 我想将变量neg指定为1(这样我可以判断最终结果是否应该返回负数)然后我想移动到下一个字符来进行实际的双重计算而不是.EX)

    int x = getchar();
      int neg = 0;

      if(x == '-') {
      neg = 1;
    x = getchar(); // will this make it so the next time I use the x        
      }            // variable it will be past the negative sign and to the
                   //first actual digit?
Run Code Online (Sandbox Code Playgroud)

c getchar

5
推荐指数
1
解决办法
726
查看次数

使用c中的free()进行分段错误

我试图用我的一些C代码找到问题.调试器说当我尝试从指针释放mem时发生错误:

int main(int argc, char *argv[]){   
    char *desc = malloc(30 * sizeof(char));
    if(desc == NULL)                    
    {
        fprintf(stderr, "Error - cannot allocate memory\n");
    }
    desc = "Debug this program.";
    printf("Description: %s\n", desc);
    free(desc);//break point here
    int cpid = fork();
    ....
Run Code Online (Sandbox Code Playgroud)

c memory free segmentation-fault

-1
推荐指数
1
解决办法
809
查看次数

标签 统计

c ×2

free ×1

getchar ×1

memory ×1

segmentation-fault ×1