Thread: Problem passing a pointer from a function back to main

  1. #1
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95

    Problem passing a pointer from a function back to main

    I have searched online, and reviewed the functions section of the text I am following. Still can not figure out why I can not point a pointer in main to the content created in a function. Here is my code:

    Code:
    #include <stdio.h>
    char* monthName ()
    {
    char month [20] = "January";
    char* pMonth;
    pMonth = month;
    printf("Printing month from monthName function %s\n", month);
    /*while (*pMonth != '\0')
      {
        putch(*pMonth);
        pMonth++;
      }*/
    return pMonth;
    }
    int main (void)
    {
    char* monthName();
    char* currentMonth;
    currentMonth = monthName();
    putch('\n');
    while (*currentMonth != '\0')
      {
        putch(*currentMonth);
        currentMonth++;
      }
    }
    Here is the result of GDB...
    Code:
    Starting program: c:\users\sansari\documents\source\programming_in_C/132.exe
    [New Thread 7920.0x28c4]
    [New Thread 7920.0x1b64]
    Breakpoint 1, monthName () at 132.c:4
    4       char month [20] = "January";
    (gdb) p month
    $1 = " \022@\000\000\000\000\000 \030@\000à_a\000\030ÿa"
    (gdb) p month[0]
    $2 = -96 ' '
    (gdb) n
    6       pMonth = month;
    (gdb) p month
    $3 = "January", '\000' <repeats 12 times>
    (gdb) n
    7       printf("Printing month from monthName function %s\n", month);
    (gdb) p pMonth
    $4 = 0x61fee8 "January"
    (gdb) n
    Printing month from monthName function January
    13      return pMonth;
    (gdb) n
    14      }
    (gdb) n
    main () at 132.c:20
    20      putch('\n');
    (gdb) n
    21      while (*currentMonth != '\0')
    (gdb) p currentMonth
    $5 = 0x61fee8 ""
    (gdb) n
    26      }
    (gdb) p currentMonth
    $6 = 0x61fee8 ""
    (gdb)

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    The problem isn't that you can't point to it, you obviously can. But since it isn't static, thus being held temporarily on the stack, there's no guarantee that it'll be there after the function returns. Undefined behavior and such.
    Devoted my life to programming...

  3. #3
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    Quote Originally Posted by GReaper View Post
    The problem isn't that you can't point to it, you obviously can. But since it isn't static, thus being held temporarily on the stack, there's no guarantee that it'll be there after the function returns. Undefined behavior and such.
    Thanks. I switched my strategy, but now I am running into another issue. I have looked online, and I seem to be doing what is validated on different posts. But I get the error message, which says:

    [CODE][$ gcc 132.c -o 132
    132.c:2:16: error: unknown type name 'month'
    void getMonth (month x)
    ^/CODE]

    Here is my implementation
    Code:
    #include <stdio.h>
    void getMonth (month x)
    {
    }
    int main (void)
    {
      enum month { January = 1, February = 2, March , April, May, June, July, August, September, October, November, December };
      enum month aMonth;
      int days;
      printf ("Enter month number: ");
      scanf ("%i", &aMonth);
      switch (aMonth) {
        case January:
        case March:
        case May:
        case July:
        case August:
        case October:
        case December:
                   days = 31;
                   break;
        case April:
        case June:
        case September:
        case November:
                    days = 30;
                    break;
        case February:
                    days = 28;
                    break;
         default:
               printf ("bad month number\n");

  4. #4
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    This is basically what I am trying to do: How to pass ENUM as function argument in C - Stack Overflow

  5. #5
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    Quote Originally Posted by zolfaghar View Post
    This is basically what I am trying to do: How to pass ENUM as function argument in C - Stack Overflow
    I found the solution to my problem. I had to define the enum globally.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-14-2013, 08:33 PM
  2. Replies: 2
    Last Post: 06-04-2013, 08:25 AM
  3. Passing array back to main function
    By greg677 in forum C Programming
    Replies: 11
    Last Post: 05-01-2010, 04:27 PM
  4. Problem with passing back pointer pointed to string
    By whichet in forum C Programming
    Replies: 9
    Last Post: 11-21-2007, 07:55 AM
  5. Passing a vector back to main
    By treenef in forum C++ Programming
    Replies: 4
    Last Post: 12-23-2005, 02:04 PM

Tags for this Thread