NO

Author Topic: Need help please ptr trouble  (Read 2554 times)

Sascha8192

  • Guest
Need help please ptr trouble
« on: August 13, 2011, 10:56:26 PM »
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100

char *eingabe(char *str) {
   char input[MAX];

   printf("Bitte \"%s\" eingeben: ",str);
   fgets(input, MAX, stdin);
   return strtok(input, "\n");
}

int main(void) {
   char *ptr;

   ptr = eingabe("Vorname");
   printf("Hallo %s\n", ptr);
   ptr = eingabe("Nachname");
   printf("%s, interssanter Nachname\n", ptr);
   return EXIT_SUCCESS;
}

Where is my mistake. pellesc 6.50.8 rc4

CommonTater

  • Guest
Re: Need help please ptr trouble
« Reply #1 on: August 14, 2011, 12:44:13 AM »
The array input[max] is created on your program's stack inside the function.  When the function returns the function's memory is released, the array is destroyed and the variable ptr used in main is no longer valid. It's about "Scope"... basically anything you do after { is only valid until }

Here's a little example of why you should never try to return an array from a function... (the same rule applies for char, int, whatever)

Code: [Select]
#include <stdio.h>

int* MyFunction(int a, int b, int c)
  {  static int array[3];
     array[0] = a;
     array[1] = b;
     array[2] = c;
     return array;  } // return a pointer.

int main (void)
  { int *a1, *a2;  // int pointers

    printf("calling a1 = MyFunction(10,20,30);\t");
    a1 = MyFunction(10,20,30);
    printf("a1 has %d %d %d\n",a1[0],a1[1],a1[2]);

    printf("calling a2 = MyFunction(100,200,300);\t");
    a2 = MyFunction(100,200,300);
    printf("a2 has %d %d %d\n",a2[0],a2[1],a2[2]);

    printf("\nLooks good, except...\t");
    printf("a1 now has %d %d %d\n",a1[0],a1[1],a1[2]);

    getchar();
    return 0; }

Type this up and try it both with and without the static keyword for the array...
« Last Edit: August 14, 2011, 12:47:47 AM by CommonTater »

Sascha8192

  • Guest
Re: Need help please ptr trouble
« Reply #2 on: August 14, 2011, 01:47:30 AM »
ok
(the same rule applies for char, int, whatever)

I thought, is there a pointer exception
but there is so static ..

Thank you that you have helped me.

« Last Edit: August 14, 2011, 01:56:47 AM by Sascha8192 »

CommonTater

  • Guest
Re: Need help please ptr trouble
« Reply #3 on: August 14, 2011, 02:27:38 AM »
ok
(the same rule applies for char, int, whatever)

I thought, is there a pointer exception
but there is so static ..

Thank you that you have helped me.

No there's no pointer exception.  C cannot return an array from a function.  That's why you see all the string functions passing buffer pointers in. 

If you have a C textbook or even a downloaded tutorial you should review the rules of scope for C... they're a real nuisance if you don't understand them... but understanding them lets you use them to advantage, doing some pretty amazing stuff.

Note that even with a static array in the example I gave you, it still messed up big time...



« Last Edit: August 14, 2011, 02:29:12 AM by CommonTater »