Thread: Having trouble accessing pointer data

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    3

    Having trouble accessing pointer data

    I wrote the following code to test out pointers to arrays. It's supposed to print the contents of array b, but instead it just prints a string of zeros. Is there some mistake here?

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<math.h>
    
    
    double* pointer(double *p1)
    {
        int n=10,i;
        double b[n],*p2;
        p2=&b[0];
            
        for(i=0;i<10;i++)
        {
            b[i]=*(p1+i)+1;
        }
        return p2;
    }
    
    
    int main()
    {
        int n=10,i;
        double a[n];
        double *p1,*p2;
        p1=&a[0];
        
        for(i=0;i<10;i++)
        {
            a[i]=i;
        }
        p2=pointer(p1);
        
        i=0;
        while(i<10)
        {
            printf("%f\n",*(p2+i));
            i++;
        }
        
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You're returning a pointer to a local variable.
    The variable goes out of scope, so your pointer is dangling.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Well for starters you can't return a local pointer.

    Next be careful with those VLA, not all versions of the C standard support this "feature".

  4. #4
    Registered User
    Join Date
    Jul 2018
    Posts
    3
    So, if I want to keep the same structure, all I need to do is define a global variable (and the change the program appropriately) to hold the pointer value?

    Next be careful with those VLA, not all versions of the C standard support this "feature".
    I did check the contents of array a and b, and those seem to be working fine, so I guess it's supported.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > So, if I want to keep the same structure, all I need to do is define a global variable (and the change the program appropriately) to hold the pointer value?
    There are two better ways.

    1. Declare the 2nd array in main and pass that as a 2nd parameter to your function.

    2. Use malloc() in your function to allocate the result array, and return that. But you have to call free() on the pointer when you're done.

    Global variables are almost always a lazy hack which eventually comes back to bite you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jul 2018
    Posts
    3
    Ah, okay. Thank you both for the replies.

  7. #7
    Registered User
    Join Date
    Oct 2017
    Posts
    36
    Again try to make use of meaningful names for your variables. It goes a long way in helping you debug your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble accessing array
    By yamahabob in forum C Programming
    Replies: 3
    Last Post: 10-30-2010, 01:28 PM
  2. Replies: 1
    Last Post: 09-20-2010, 11:17 AM
  3. Having trouble accessing members of a struct
    By Swarvy in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2009, 08:47 AM
  4. Trouble accessing this site....
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-23-2002, 09:26 AM

Tags for this Thread