gpt4 book ai didi

c - 警告 : assignment from incompatible pointer type [enabled by default]

转载 作者:太空宇宙 更新时间:2023-11-04 06:24:58 25 4
gpt4 key购买 nike

我的源代码:

#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]);
}
}

我正在尝试创建一个动态的二维数组(数组名称: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 */

最佳答案

在您的代码中,去除错误的转换

此外,在为 matrix 分配内存时,您必须使用 sizeof(int *)

改变

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 - 警告 : assignment from incompatible pointer type [enabled by default],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28345905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com