I have an assignment that requires me to not use an array with [] at all throughout my code, read a file in a certain way, and I need a better understanding with malloc as well with pointers. The code with the [] is pretty easy for me, but pointers confuses me. I don't expect every single one of these to be answered which is fine.

For example:
/*input from datafile.dat*/
3
2
4
1 2
3 4
5 6 7 8

I understand that I need to get all that in the array which I got down. Array[2] should be 4 as

#define array 80
....
void matrices(int **A, int **B, int **C,  int *m, int *n, int *p, char *str){
...
char *pa= array;
8/
p =*(pa+2) /* is 4?
...
}

As for the arrary with [] to an array with no [], if someone can help me convert this code to no [], I can take care of everything else in my program. Where x,y are the dimensions of a 2d matrix.

A[x][y] =(x*2)+ y + 1;

Lastly, I see this kind of codes for malloc, but I am confused on what it does.

if(((*A = malloc((r)*(c)*sizeof(int))==NULL)

*A = malloc((r)*(c)*sizeof(int))
/* *A = A[r][c] ?*/

*p=malloc(80*sizeof(int))
/* I understand this as 80 ints to be returned

s=malloc(100)
/*get 100 bytes?

This might help

#include<stdio.h>

int main()
{
    int ary[10] = {1,3,5,8,12,54,2,77,49,12};
    int *ptr;
    int  i;
    
    
    ptr = ary;  // set the pointer to the address of the array

    for (i=0; i<10; i++)    // add the index to the pointer
    {
        printf("%d ", *(ptr+i));  // ptr remains unchanged
    }
    printf("\n");           


    for (i=0; i<10; i++)    // increment the pointer itself
    {
        printf("%d ", *ptr++);  // ptr is changed
    }
    printf("\n");
    
    return 0;
}

Got that sucker to do matrices. Now to multiply is the problem using no brackets.

The code I have for multiplying is: (where m=3, n= 2, p=4, A = 3x2 matrix, B= 2x4 matrix)

for(i=0; i<m; i++){
     for(j=0; j<p; j++){
         C[i][j] =0;
               for(k=0; k<n; k++)
                     C[i][j] += A[j][k]*B[k][j];
      }
}

/*should I do this for C?*/
C =(malloc((i)*(j)*sizeof(int))==0);

There is an obvious formula for moving from one column to another and one row to another if your matrix is designed as a 1D-array.
Consider this 3x4 matrix: *matrix = {1,2,3,4,5,6,7,8,9,10,11,12} What does this matrix look like in row/col form?
What is the contents of
-- row 1?
-- col 1?
-- cell 2,3

From that info, you can create a formula to determine what number to replace [][] with that can be added to the pointer itself, as in the first loop I posted above.

This article has been dead for over six months. Start a new discussion instead.