警告:分配从兼容的指针类型[默认启用]
我的源代码:警告:分配从兼容的指针类型[默认启用]
#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 */
删除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);
当然,也在其他声明中。 – meskobalazs 2015-02-05 14:05:53
更改后,我也得到了像这样的警告:赋值使得整型指针没有强制转换[默认启用] matrix [i] =(int)malloc(sizeof(int)* columns); – user3815361 2015-02-05 14:06:05
@meskobalazs;你没有得到这个:“*矩阵[我]是指向int类型的指针。*”? – haccks 2015-02-05 14:06:58
在代码中,摆脱了错误铸件的。
此外,您必须使用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()
。
请注意,C中的任何数组都是一个指针。你应该把你的矩阵看作一个数组数组,所以你应该在第一个赋值中分配'sizeof(int *)'。 – Mauren 2015-02-05 14:06:12
@Mauren数组不是指针。 – Quentin 2015-06-25 11:20:24