我的源代码:
#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] = ' ';
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);
引用:不强制转换malloc()
的返回值。
从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);