2015-02-05 109 views
0

我的源代碼:警告:分配從兼容的指針類型[默認啓用]

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

struct node 
{ 
    char string_name[30]; 
    int string_value; 
    struct node *next; 
}; 

struct node *root = NULL,*current; 

int **matrix; 
int *last_column; 

int count = 0; //this for store values into last_column array 

int k=0,l=0,rows=1,columns=1; // k & l variables are used to store values in matrix 

void no_rows_columns(char filename[]) // this function count total number of rows&columns 
{ 
    FILE *fp; 
    char ch; 
    int i; 
    fp = fopen(filename, "r"); 
    if (fp == NULL) 
    { 
     printf("Could not open file %s", filename); 
     exit(0); 
    } 
    while((ch = fgetc(fp)) != EOF) 
    { 
     if(ch == 32) 
     { 
      columns++; 
     } 
     if(ch == '\n') 
     { 
      break; 
     } 
    } 
    while((ch = fgetc(fp)) != EOF) 
    { 
     if(ch == '\n') 
     { 
      rows++; 
     } 
    } 
    matrix = (int *) malloc(sizeof(int) * rows); /* Row pointers */ 
    for (i = 0; i < rows; i++) 
    { 
     matrix[i] = (int) malloc(sizeof(int) * columns); /* Rows */ 
    } 
    last_column = (int *)malloc(sizeof(int)*rows);//this matrix is created for last_column store 
} 

void dispaly_structure() //this function used to display strings and its values 
{ 
    struct node *temp; 
    temp = root; 
    while(temp!=NULL) 
    { 
     //printf("String: %s \t Value: %d \n",temp->string_name,temp->string_value); 
     temp = temp->next; 
    } 
} 


int search(int value) // this function is used to search weather string alrady prasent in structre or not 
{ 
    struct node *temp = root; 
    while(temp != NULL) 
    { 
     if(temp->string_value == value) 
     { 
      return 1; 
      break; 
     } 
     else 
     { 
      temp = temp->next; 
     } 
    } 
} 

void store(char ch[],int int_value) // this function is used to store strings and its values 
{ 
    struct node *temp; 

    if(root == NULL) 
    { 
     temp = (struct node*)malloc(sizeof(struct node)); 
     strncpy (temp->string_name, ch, 30); 
     temp->string_value = int_value; 
     temp->next = NULL; 
     current = temp; 
     root = temp; 
    } 
    else 
    { 
     int return_value = search(int_value); 
     if(return_value != 1) 
     { 
      temp = (struct node*)malloc(sizeof(struct node)); 
      strncpy (temp->string_name, ch, 30); 
      temp->string_value = int_value; 
      temp->next = NULL; 
      current->next = temp; 
      current = temp; 
     } 
    } 
} 


void Display(char ch[]) // this function is used to create final array with string integer values 
{ 
    int i,len,result=0; 
    len = strlen(ch); 
    for(i=0; i<len-1; i++) 
    { 
     result = result+ch[i]; 
    } 
    if(l == columns-1) 
    { 
     last_column[count] = result; 
     count++; 
    } 
    if(l == columns) 
    { 
     l = 0; 
     k++; 
    } 
    matrix[k][l] = result; 
    l++; 
    store(ch,result); 
    //printf("String Output:%s \t int_value :%d \n",ch,result); 

} 

void main() 
{ 
    char string[20]; 
    int i=0,count=0,j; 

    FILE *fp; 
    char filename[25],ch; 
    printf("Enter ur file name \n"); 
    scanf("%s",filename); 
    no_rows_columns(filename); // calling function to get no.columns&rows 

    fp = fopen(filename,"r"); 

    while((ch = fgetc(fp)) != EOF) 
    { 
     string[i] = ch; 
     i++; 
     string[i] = '\0'; 
     if(ch == 32 || ch == '\n') 
     { 
      Display(string); // calling display function 
      count++; 
      i = 0; 
     } 
    } 
    dispaly_structure(); // calling function to see string and its value 

    printf("final Matrix \n"); //display final matrix 
    printf("-----------------------------------\n"); 
    for(i=0; i<14; i++) 
    { 
     for(j=0; j<5; j++) 
     { 
      printf("%d ",matrix[i][j]); 
     } 
     printf("\n"); 
    } 
    printf("\n"); 
    printf("Last column \n"); 
    for(i=0; i<rows; i++) 
    { 
     printf("%d \n",last_column[i]); 
    } 
} 

我試圖創建一個動態的,2維數組(數組名:matrix),但我獲得兩個警告,我無法解決這些問題,該警告是:

assignment from incompatible pointer type [enabled by default] 
matrix = (int *) malloc(sizeof(int) * rows); /* Row pointers */ 

assignment makes pointer from integer without a cast [enabled by default] 
matrix[i] = (int) malloc(sizeof(int) * columns); /* Rows */ 
+0

請注意,C中的任何數組都是一個指針。你應該把你的矩陣看作一個數組數組,所以你應該在第一個賦值中分配'sizeof(int *)'。 – Mauren 2015-02-05 14:06:12

+0

@Mauren數組不是指針。 – Quentin 2015-06-25 11:20:24

回答

1

刪除CA ST自malloc特意從聲明

matrix[i] = (int) malloc(sizeof(int) * columns); 
      // ^^matrix[i] is of pointer to int type. 

也改變

matrix = (int *) malloc(sizeof(int) * rows); 

matrix = malloc(sizeof(int *) * rows); 
+0

當然,也在其他聲明中。 – meskobalazs 2015-02-05 14:05:53

+0

更改後,我也得到了像這樣的警告:賦值使得整型指針沒有強制轉換[默認啓用] matrix [i] =(int)malloc(sizeof(int)* columns); – user3815361 2015-02-05 14:06:05

+0

@meskobalazs;你沒有得到這個:「*矩陣[我]是指向int類型的指針。*」? – haccks 2015-02-05 14:06:58

3

在代碼中,擺脫了錯誤鑄件的。

此外,您必須使用sizeof(int *),同時將內存分配給matrix

變化

matrix = (int *) malloc(sizeof(int) * rows); 

matrix = malloc(sizeof(int *) * rows); 

matrix[i] = (int) malloc(sizeof(int) * columns); 

matrix[i] = malloc(sizeof(int) * columns); 

參考號:do not cast返回值爲malloc()

相關問題