Thread: help with pointers

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    6

    Question help with pointers

    I am trying to understand how to return and pass the char pointer between the funtions.

    My sample code is:
    Code:
    #include<stdio.h>
    #include<string.h>
    char* input();
    void display(char*);
    int main()
    {
     char *acc_no=input();
     printf("The account number is from outside the input function %s \n",acc_no);
     display(acc_no);
     return 0;
    }
    char* input()
      {
        char *acc_no=NULL;
        char acc_num[10];
        printf("enter the account_no \n");
        fgets(acc_num,10,stdin);
        printf("The account number is %s \n",acc_num);
        acc_no = acc_num;
        printf("The account number is from the pointer %s \n",acc_no);
        return (acc_no);
     }
    void display(char *acc_no)
     { 
       printf("The account nummber is %s \n",acc_no);
     }
    The output is

    enter the account_no
    12313
    The account number is 12313
    The account number is from the pointer 12313
    The account number is from outside the input function 12313
    The account nummber is ÿÂ


    The funtion input is returning char pointer correctly.
    But while passing char pointer to the function display, it is passing some garbage value....

    pls explain me what is the mistake....

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by priyaxen View Post
    I am trying to understand how to return and pass the char pointer between the funtions.

    My sample code is:
    Code:
     char* input()
      {
        char *acc_no=NULL;
        char acc_num[10];
        printf("enter the account_no \n");
        fgets(acc_num,10,stdin);
        printf("The account number is %s \n",acc_num);
        acc_no = acc_num;
        printf("The account number is from the pointer %s \n",acc_no);
        return (acc_no);
     }
    One problem is here. You are allocating 10 bytes inside your function on the stack and then trying to return a pointer to it. Problem is: these allocations become invalid as soon as the function returns. A quick solution would be to allocate on the heap using static

    Code:
    static char acc_num[10];
    With this declaration, there is only one allocation done, and a pointer to this will be valid. Another solution would be to declare the array outside this function and pass a pointer into your input function

    Code:
    char *input(char *arr, size_t arr_sz);
    
    int main() {
    
        char acc_num[10];
        input(acc_num, 10);
        display(acc_num);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 43
    Last Post: 05-23-2013, 03:01 PM
  2. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  3. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  4. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  5. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM