2017-04-21 44 views
0

我的代码是这样的时候:程序崩溃试图运行我的代码的特定部分

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

char ** djiksin(char ** inp){ 

} 
int evaluation(char ** rpn){ 

} 
void printarr(char arr[]){ 
    int i=0; 
    while(arr[i] != '\0'){ 
     printf("%c",arr[i]); 
     ++i; 
    } 
} 
int main(){ 
    int i = 0,k = 0,j=0; 
    char a[201] = {'\0'}; 
    char ** strinp; 
    fgets(a,201,stdin); 
    while(a[i] != '\0'){ 
     if(a[i] == '(' || a[i] == ')' || a[i] == '*'|| a[i] == '+'|| a[i] == '^'|| a[i] == '~'|| a[i] == '/'|| a[i] == '-'){ 
      j = 0; 
      k = k+1; 
      strinp[k][j] = a[i]; 
      k = k + 1; 
     }else if(a[i] == ' '){ 
      k = k + 1; 
      j = 0; 
     }else{ 
      strinp[k][j] = a[i]; 
      j = j+1; 
     } 
     ++i; 
    } 
    printarr(a); 
    return 0; 
} 

代码的用途迄今采取mathmethical公式,并格式化为使用调车场alghorithm。这在我脑海中试图让每个操作符和操作数分开。 最后,strictp应该给出一串字符串,我可以在我之前声明的函数中使用它。

但每次我尝试编译&运行测试程序后,我把输入窗口给出的错误“程序停止工作”。关闭错误后,该输出在命令提示符:Process exited after 3.871 seconds with return value 3221225477

当我在main发表意见while部分代码工作只是适当的输出罚款。

有什么想法?

+0

最简单的解决方案是使striinp成为一个二维阵列。类似于strictp [100] [201]。 –

回答

1
char ** strinp; 
... 
strinp[k][j] = a[i]; 

strinp是一个未初始化的指针。你没有为它分配任何内存,并且你没有指向有用的内存。解除引用无效指针是导致崩溃的原因。

0

您不会为您的strictp变量分配内存。

char ** strinp = (char **)malloc(sizeof(char*) * 20); 

for(int i = 0; i < 20; i++) 
{ 
    strinp[i] = (char *)malloc(sizeof(char) * 20); 
} 

你需要的是这样的。当然,您需要根据需要更改分配大小。

0

正如其他人说你必须在尝试为srtinp pionter赋值之前分配内存。

/* 

compile ::: $ gcc test.c -o test 

run ::: $ ./test 

*/ 

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


void 
printarr(char arr[]) 
{ 
    int i=0; 
    while(arr[i] != '\0'){ 
     printf("%c",arr[i]); 
     ++i; 
    } 
} 


int 
main(int argc, char **argv) 
{ 
    int i = 0,k = 0,j=0; 
    char a[201] = {'\0'}; 
    char ** strinp; 

    printf("Enter a string\n"); 
    fgets(a,201,stdin);///get input from user 


/// obtain length of input 
    int x=-1; 
    for (x=0 ; a[x]!='\0' && x<=200;x++){ 
      ; 
    } 
    x--; 

    printf("lenght of a: %d\n",x);///len a is x 



/// allocate sufficient amount of Memory 
strinp = malloc(sizeof(char*) * x); 
for(int i=0;i<x; i++) 
    strinp[i] = malloc(sizeof(char)*x); 




//~ i=0; 
//~ j=0; 
//~ k=0; 

    /// do this 
    ///seem to erase space from input 
    while(a[i+1] != '\0'){ 
     //printf("in while1\n"); 
     if(a[i] == '(' || a[i] == ')' || a[i] == '*'|| a[i] == '+'|| a[i] == '^'|| a[i] == '~'|| a[i] == '/'|| a[i] == '-'){ 
      j = 0; 
      k = k+1; 
      //printf("in while2\n"); 
      strinp[k][j] = a[i]; 
      //printf("%c,%c\n",a[i],strinp[k][j]); 
      k = k + 1; 
      //printf("%c\n",a[i]); 
     }else if(a[i] == ' '){ 
      k = k + 1; 
      j = 0; 
      // printf("%c\n",a[i]); 
     }else{///if alphabet or number 
      //printf("in while 3\n"); 
      strinp[k][j] = a[i]; 
      //printf("%c,%c\n",a[i],strinp[k][j]); 
      j = j+1; 
     } 
     ++i; 
    } 

    printf("array:"); 
    printarr(a);///print array of a[] 
    //printf("end\n"); 

    ///print strinp 
    printf("pointer:"); 
    for(int i=0;i<x;i++) 
    for(int j=0;j<x;j++) 
     printf("%c",strinp[i][j]); 

    printf("\n");  

    ///free aloocated memory 
    for(i=0;i<x; i++) 
     free(strinp[i]); 
    free(strinp); 

    return 0; 

}