0

My source code:

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

I'm trying to create a dynamic, 2-dimensional array (array name : matrix), but I'm getting two warnings, and I'm unable to resolve these, the warnings are:

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

and

assignment makes pointer from integer without a cast [enabled by default]
matrix[i] = (int) malloc(sizeof(int) * columns); /* Rows */
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
user3815361
  • 23
  • 1
  • 10
  • Please note that any array in C is a pointer. You should think of your matrix as an array of arrays, so you should be allocating `sizeof(int*)` in the first assignment. – Mauren Feb 05 '15 at 14:06
  • @Mauren Arrays are not pointers. – Quentin Jun 25 '15 at 11:20

2 Answers2

3

In your code, get rid of the erroneous castings.

Also, you've to use sizeof(int *) while allocating memory to matrix.

change

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

to

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

and

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

to

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

Reference : do not cast the return value of malloc().

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

Remove cast from malloc specially from the statement

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

also change

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

to

matrix = malloc(sizeof(int *) * rows);
haccks
  • 104,019
  • 25
  • 176
  • 264