Thread: Assigning pointers to array in function

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    21

    Assigning pointers to array in function

    Hello!

    I have this code:

    Code:
    #include <stdio.h>
    
    void assignPoint(int [], int*, int*);
    void printPoint(int*, int*);
    
    int main(void){
        int array[3];
        int* p1;
        int* p2;
        assignPoint(array, p1, p2);
        printPoint(p1, p2);
    }
    
    void assignPoint(int array[], int* p1, int* p2){
        p1 = &array[1];
        p2 = &array[2];
    }
    
    void printPoint(int* p1, int* p2){
        printf("%d %d", *p1, *p2);
    }
    I am getting segmentation here. My question is: Do pointers keep their values after function returns or do I have to return them?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Do pointers keep their values after function returns or do I have to return them?
    By default everything is passed by value (including pointers), so in your example your assignPoint() function is working with copies of the pointer so you can't change where the pointer is pointing.

    By the way your compiler should be able to warn you about the problems:
    ||=== Build: Debug in test.c (compiler: GNU GCC Compiler) ===|
    main.c||In function ‘assignPoint’:|
    main.c|14|warning: parameter ‘p1’ set but not used [-Wunused-but-set-parameter]|
    main.c|14|warning: parameter ‘p2’ set but not used [-Wunused-but-set-parameter]|
    main.c||In function ‘main’:|
    main.c|10|warning: ‘p1’ is used uninitialized in this function [-Wuninitialized]|
    main.c|10|warning: ‘p2’ is used uninitialized in this function [-Wuninitialized]|
    ||=== Build finished: 0 error(s), 4 warning(s) (0 minute(s), 2 second(s)) ===|
    If you want to change where the pointer is pointing you need something like:

    Code:
    #include <stdio.h>
    
    void assignPoint(int [], int**, int**);
    void printPoint(int*, int*);
    
    int main(void){
        int array[3];
        int* p1;
        int* p2;
        assignPoint(array, &p1, &p2);
        printPoint(p1, p2);
    }
    
    void assignPoint(int array[], int** p1, int** p2){
        *p1 = &array[1];
        *p2 = &array[2];
    }
    
    void printPoint(int* p1, int* p2){
        printf("%d %d", *p1, *p2);
    }
    And don't forget you need to initialize the elements of the array before you get some meaningful information.


    Jim
    Last edited by jimblumberg; 10-11-2016 at 08:57 AM.

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    21
    [SPOILER]
    Quote Originally Posted by jimblumberg View Post
    By default everything is passed by value (including pointers), so in your example your assignPoint() function is working with copies of the pointer so you can't change where the pointer is pointing.

    By the way your compiler should be able to warn you about the problems:


    If you want to change where the pointer is pointing you need something like:

    Code:
    #include <stdio.h>
    
    void assignPoint(int [], int**, int**);
    void printPoint(int*, int*);
    
    int main(void){
        int array[3];
        int* p1;
        int* p2;
        assignPoint(array, &p1, &p2);
        printPoint(p1, p2);
    }
    
    void assignPoint(int array[], int** p1, int** p2){
        *p1 = &array[1];
        *p2 = &array[2];
    }
    
    void printPoint(int* p1, int* p2){
        printf("%d %d", *p1, *p2);
    }
    And don't forget you need to initialize the elements of the array before you get some meaningful information.


    Jim
    [/SPOILER]
    Thanks!

    Yes my compiler warn me but I wrote the code on another computer (which didnt have internet access) so I had to rewrite the code in chrome

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 43
    Last Post: 05-23-2013, 03:01 PM
  2. assigning 2D arrays to pointers
    By rakeshkool27 in forum C Programming
    Replies: 5
    Last Post: 01-19-2010, 12:34 AM
  3. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  4. Replies: 9
    Last Post: 09-19-2007, 02:26 AM
  5. Assigning pointers from pointers in struct.
    By Brian in forum C Programming
    Replies: 2
    Last Post: 10-18-2003, 04:30 AM

Tags for this Thread