Thread: Memset with 2d arrays

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    5

    Question Memset with 2d arrays

    Hi folks. I am currently confusing myself over why the first code doesn't work when the second code works. Would someone explain to me? The differences in codes have been bolded out.

    Code:
    typedef struct adjMat{
        int matrix[20][20];
        int vertexes;
    }adjMat;
    
    void addEdge(adjMat* adjMat, int node1, int node2){
        int** matrix = adjMat -> matrix;
        matrix[node1][node2] = 1;
        matrix[node2][node1] = 1;
    }
    
    void showGraph(adjMat* adjMat){
        int v = adjMat->vertexes;
        int** mat = adjMat->matrix;
        for(int i = 1; i <=v; i++){
            printf("Vertex %d is connected to ... ", i);
            for(int j = 1; j <= v; j++){
            printf("%d, ", adjMat->matrix[i][j]);
            }
        }
    }
    
    int main()
    {
    
        adjMat mat;
        mat.vertexes = 8;
        for(int i = 1; i <= mat.vertexes; i ++){
            memset(mat.matrix[i][1], 0x00, sizeof(int)*9);
        }
        showGraph(&mat);
        return 0;
    }
    Code:
    ......
    int main()
    {
    
        adjMat mat;
        mat.vertexes = 8;
        for(int i = 1; i <= mat.vertexes; i ++){
            memset(mat.matrix[i], 0x00, sizeof(int)*9);
        }
        showGraph(&mat);
        return 0;
    }
    The only difference is with the argument mat.matrix[i] vs mat.matrix[i][1] to the memset function.

    Also, I realized that when I degrade the 2d array variable matrix into double pointer in the showGraph() function, and then try to access the value from that double pointer, the code does not work.

    Code:
    void showGraph(adjMat* adjMat){
        int v = adjMat->vertexes;
        int** mat = adjMat->matrix;
        for(int i = 1; i <=v; i++){
            printf("Vertex %d is connected to ... ", i);
            for(int j = 1; j <= v; j++){
            printf("%d, ", mat[i][j]);
            }
        }
    }
    Thanks for helping out!
    Last edited by crystalize123; 04-21-2019 at 08:27 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    The first one doesn't work because "mat.matrix[i][1]" isn't a pointer. memset expects a pointer as its first argument.

    Quote Originally Posted by crystalize123 View Post
    Also, I realized that when I degrade the 2d array variable matrix into double pointer in the showGraph() function, and then try to access the value from that double pointer, the code does not work.
    Of course, since you're trying to assign an "int[][]" to a "double**". The compiler objects heavily when you try to assign pointers of different types (if it doesn't, it should).
    Last edited by GReaper; 04-21-2019 at 10:15 PM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    5
    Quote Originally Posted by GReaper View Post
    Of course, since you're trying to assign an "int[][]" to a "double**". The compiler objects heavily when you try to assign pointers of different types (if it doesn't, it should).
    Oops, it's an typo. I assigned an "int[][]" to a "int**", as shown here with int** mat.

    Code:
    void showGraph(adjMat* adjMat){
        int v = adjMat->vertexes;
        int** mat = adjMat->matrix;
        for(int i = 1; i <=v; i++){
            printf("Vertex %d is connected to ... ", i);
            for(int j = 1; j <= v; j++){
            printf("%d, ", mat[i][j]);
            }
        }
    }
    Would you tell me why mat[i][j] won't work?
    And also what would "mat.matrix[i][1]" be if it's not a pointer?
    Last edited by crystalize123; 04-21-2019 at 10:31 PM.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    Quote Originally Posted by crystalize123 View Post
    Would you tell me why mat[i][j] won't work?
    Because you may think that "int[][]" and "int**" are equivalent, but they are not. The compiler lets you use static arrays as pointers only for convenience, and in this case a 2D static array is entirely different compared to a double pointer.

    Quote Originally Posted by crystalize123 View Post
    And also what would "mat.matrix[i][1]" be if it's not a pointer?
    What you have declared it as, int.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memset
    By uryenugurkem in forum C Programming
    Replies: 2
    Last Post: 02-11-2018, 07:42 AM
  2. Initializing character arrays with memset
    By MarkB in forum C Programming
    Replies: 6
    Last Post: 09-02-2009, 09:40 PM
  3. how to use memset for int?
    By manav in forum C Programming
    Replies: 4
    Last Post: 04-12-2008, 07:15 AM
  4. memset
    By l2u in forum C Programming
    Replies: 3
    Last Post: 07-03-2006, 04:16 PM
  5. memset()
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-11-2002, 09:34 PM

Tags for this Thread