Thread: Passing array to function using pointers

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    16

    Passing array to function using pointers

    Hello, not long ago I started studied C and now I'm having some problems with developing a small program, that would search each row of my matrix, pick the highest element and switch it with the element on diagonal. It was no problem to do it by passing the array to a function, however the it's hard for me to understand the correct way of doing the same with pointers. I got the "Suspicious pointer conversion" error, and various others at the function defining stage. Here's my code, hope you could help me with at least defining the function:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <time.h>
    
    #define SIZE 4
    
    int vert_apstr(int array[][SIZE], int, int);
    int adr_apstr(int *arr, int, int); /* don't know how correct is this */
    
    void main(){
    int matrix[SIZE][SIZE], matrix2[SIZE][SIZE];
    int i,j;
    
    clrscr();
    randomize();
    
    printf("\n 1) Function (value) \n");
    printf("\n  Old Matrix\n\n");
    for (i=0;i<SIZE;i++){
      for (j=0;j<SIZE;j++){
       matrix[i][j]=random(10);
       printf("%3d ", matrix[i][j]);
      }
    printf("\n");
    }
    
    vert_apstr(matrix,SIZE,SIZE);
    
    gotoxy(31,5);
    printf("New matrix\n\n");
    for (i=0;i<SIZE;i++){
     gotoxy(30,i+7);
      for (j=0;j<SIZE;j++){
       printf("%3d ", matrix[i][j]);
      }
    printf("\n\n");
    }
    
    printf("\n 2) Function (adress)\n");
    
    /* something similar, but with pointers */
    
    getch();
    }
    
    int vert_apstr(int array[][SIZE], int aug_r1, int aug_r2){
    int i,j,buf;
    int hi_elem, hi_col;
    
    for (i=0; i<aug_r1; i++){
       hi_elem=array[i][0];
       hi_col=0;
      for (j=0; j<aug_r2; j++){
       if (array[i][j]>hi_elem){
        hi_elem=array[i][j];
        hi_col=j;
       }
      }
    buf=array[i][i];
    array[i][i]=array[i][hi_col];
    array[i][hi_col]=buf;
    }
    
    return array[aug_r1][aug_r2];
    
    }
    The things I want to know are:

    1)
    Code:
    int adr_apstr(int *arr, int, int);
    - prototype
    Code:
    int adr_apstr(int *arr, int aug_r1, int aug_r2){ ... }
    - the function itself
    Code:
    adr_apstr(matrix2,SIZE,SIZE);
    - call for a function

    What is the correct way, to write these 3 things?

    2) if I typed elements myself, I would have done something like this "scanf("%d",matrix2+i)", but if I use randomize(), how can I give the array a value? I tried "for (i=0;i<SIZE*SIZE;i++) (matrix2+i)=random(10);", but it just gave me another error. Glad, if you could help!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're close, but use the index:
    Code:
    for(i = 0; i < maxElement; i++)
      matrix[i] = random number you want to assign

    The thing to remember is that you *NEVER* pass an array to a function - ever. C will always "decay" that parameter, to a simple pointer.

    since the name of the array, is the address of the base of the array, the calling is done just using the name of the array:

    Code:
    myFunction(myArrayName);
    myFunction would start with:

    Code:
    (void/whatever return type) myFunction(int *myArrayName) {
    
    }
    where "int" would be whatever the data type of the array is.

    Why the * then? Because the function needs to know that this is an address it will be receiving. It has no knowledge of the array, yet. You could write it using the familiar myArrayName[] style - but it's still an address that is being received.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Can you show how you are calling the function adr_apstr()? Since array[][] is 2D, the only way you can pass it by name as a pointer to int is if you cast it to that type before invoking adr_apstr() else the array name by itself becomes a pointer to an array of ints.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Passing a 2d Array of pointers to a Function
    By miclus in forum C Programming
    Replies: 6
    Last Post: 09-11-2004, 07:34 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM