Thread: Very simple Question About Pointer Passing in C

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    1

    Very simple Question About Pointer Passing in C

    In the following code, why should I write "(int *)" for passing the matrix to the readMatrix function? Is it passing the pointer to the location of the matrix[4][4] in memorry? can I pass it using ampersand like &matrix?

    Thanks in advance


    Code:
    void p1(void)
    {
        
    int matrix[4][4]; readMatrix("matrix1.txt", (int *)matrix);
    }

    Code:
    void readMatrix(char * filename, int * matrix)
    {
        
    FILE *fp = fopen(filename, "r"); int i,j,val; for(i=0; i<4; i++) { for(j=0; j<4; j++) { fscanf(fp,"%d",&val); *((matrix+i*4)+j)=val; } } fclose(fp);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sorayaj
    why should I write "(int *)" for passing the matrix to the readMatrix function? Is it passing the pointer to the location of the matrix[4][4] in memorry?
    When an array is passed as an argument, it is converted to a pointer to its first element. matrix is an int[4][4], so when passed as an argument an int(*)[4] is passed instead, but this is not compatible with the int* parameter, hence the cast to silence the likely compiler warning.

    Quote Originally Posted by sorayaj
    can I pass it using ampersand like &matrix?
    Yes, and I feel that it would be better style:
    Code:
    readMatrix("matrix1.txt", &matrix[0][0]);
    On the other hand, readMatrix assumes a 4 by 4 matrix, so I'd argue that even better yet would be to change readMatrix:
    Code:
    void readMatrix(char *filename, int (*matrix)[4])
    {
        FILE *fp = fopen(filename, "r");
        if (fp)
        {
            int i, j, val;
    
            for (i = 0; i < 4; i++)
            {
                for (j = 0; j < 4; j++)
                {
                    if (fscanf(fp, "%d", &val) == 1)
                    {
                        matrix[i][j] = val;
                    }
                    else
                    {
                        /* record a special error value? report a read error? */
                    }
                }
            }
            fclose(fp);
        }
        else
        {
            /* report a file open error? */
        }
    }
    Now you can write:
    Code:
    void p1(void)
    {
        int matrix[4][4];
        readMatrix("matrix1.txt", matrix);
    }
    EDIT:
    Incidentally, filename should be a const char* rather than just a char* because you clearly do not intend to modify the string.
    Last edited by laserlight; 01-25-2016 at 10:39 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a string to a function - simple question
    By vegan in forum C Programming
    Replies: 9
    Last Post: 07-25-2011, 06:00 PM
  2. Pointer Passing Question
    By NuNn in forum C Programming
    Replies: 5
    Last Post: 02-13-2009, 08:58 AM
  3. simple question on passing parameters
    By mickey0 in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2007, 09:28 AM
  4. Passing by Reference. Simple question.
    By d3m105 in forum C Programming
    Replies: 6
    Last Post: 10-31-2007, 12:47 PM
  5. ! C question: simple integer function passing
    By aaronc in forum C Programming
    Replies: 10
    Last Post: 04-29-2004, 01:18 AM

Tags for this Thread