Returning a local char array ?

In this code there are four functions. One main-function, and three sub functions returning a char array. The function GetByte3 is invalid because it returns a NULL pointer. Can some smart person tell me what the return statement in function GetByte2 does that makes the copy works fine? I understand that the return statement returns the pointer ptr3 to main but does it not point to the temp that is out of scope when executions leaves GetByte2 ?



void GetByte(char *ptr)
{
char temp[100];
printf("Byte: ");
scanf("%s", temp);

int i=0, j=0;
while (temp[j] != '\0')
{
ptr[i]=temp[j]; i++; j++;
}
ptr[i] = '\0';
}

char* GetByte2(char* ptr)
{
char temp[100];
printf("Byte2: ");
scanf("%s", temp);

//printf("Debug \t: %s", temp);

ptr = &temp[0];
return ptr;

}

void GetByte3(char* ptr3)
{
char temp[100];
printf("Byte3: ");
scanf("%s", temp);

//printf("Debug \t: %s", temp);

ptr3 = &temp[0];
}


int main(int argc, char *argv[])
{
//- Function #1 ------------------------------
char byte[100];

GetByte(byte);
printf("Byte: %s\n", byte);

//- Function #2 ------------------------------
char byte2[100];
char* p = byte2;

p = GetByte2(p);
printf("Byte2: %s\n", p);

//- Function #3 ------------------------------
char byte3[100];
GetByte3(byte3);
printf("Byte3: %s\n", byte3);


system("PAUSE");
return EXIT_SUCCESS;
}
Neither function returns an array.

GetByte2() returns a poitner to a local variable (which becomes invalid once the function returns)

GetByte3() does not return or modify anything (it attempts to modify its argument, but it's passed by value, so only its local copy is modified)

(also note that scanf("%s"...) is unsafe: given access to your program, I could make it execute any code, e.g. email me all your passwords. If you must use scanf and arrays, always specify the array size for %s, e.g. scanf("%99s", temp);
closed account (4z0M4iN6)
Hi Jen, you should use the format options, so that people can see your code better. I did this for you. I also shortened function GetByte, maybe you now can see clearer.

Cubbi is right about GetByte, because, what would happen, if the input string would be longer, than 99 characters.

Cubbi is right about GetByte2. Local variables like temp[100] are allocated on the stack and will be destroyed, when the function returns . For hindering this you could allocate a static array, which will not be destroyed.

You could have written:

static char temp[100];

And Cubbi is right about GetByte3. Only local values, and the last line, which I commented, just for nothing.

Instead of:

//ptr3 = &temp[0];

you also could have written:

//ptr3 = temp;

just for nothing.

Can you see this?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
void GetByte(char *ptr)
{
	printf("Byte: ");
	scanf("%s", ptr);
}

char * GetByte2(void)
{
	char temp[100];
	printf("Byte2: ");
	scanf("%s", temp);
	return temp;
}

void GetByte3(char* ptr3)
{
	char temp[100];
	printf("Byte3: ");
	scanf("%s", temp);
	//ptr3 = &temp[0];
}


int main(int argc, char *argv[])
{
	//- Function #1 ------------------------------
	char byte[100];

	GetByte(byte);
	printf("Byte: %s\n", byte);

	//- Function #2 ------------------------------
	char byte2[100];
	char* p = byte2;

	p = GetByte2();
	printf("Byte2: %s\n", p);

	//- Function #3 ------------------------------
	char byte3[100];
	GetByte3(byte3);
	printf("Byte3: %s\n", byte3);


	system("PAUSE");
	return EXIT_SUCCESS;
}



What youd could have meant by GetByte3:

1
2
3
4
5
6
7
void GetByte3(char * * ptr3)
{
	static char temp[100];
	printf("Byte3: ");
	scanf("%s", temp);
	*ptr3 = temp;
}


and

1
2
3
4
	//- Function #3 ------------------------------
	char * byte3;
	GetByte3(&byte3);
	printf("Byte3: %s\n", byte3);


But I don't know, whether you can think this clearly over

And also some lines of Function 2 in main don't make much sense. Could have been written simply as:

1
2
	//- Function #2 ------------------------------
	printf("Byte2: %s\n", GetByte2());


But with some practice you soon will become better

And please use the format option <> next time
Last edited on
Thanks Cubbi and dadabe, I appreceiate your comments!
But...

Following code compiles and runs fine on my computer. And I would like to know why. GetByte2 returns (to main) a pointer to a char array and I would like to know why the return works fine? Because I would like to think that the pointer that GetByte2 returns will point to temp[100] that is out of scope when executions leaves GetByte2? But I still get a result that is fine...why?

/JEN :-)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
char* GetByte2(char* ptr)
{
  char temp[100];
  printf("Byte2: "); 
  scanf("%s99", temp); 

  //printf("Debug \t: %s", temp); 

  ptr = &temp[0];
  return ptr;
}


int main(int argc, char *argv[])
{
 
  //- Function #2 ------------------------------
  char byte2[100]; 
  char* p = byte2;
 
  p = GetByte2(p);   printf("Byte2: %s\n", p);

}
Your program has undefined behavior. It works only because your program is small and the memory which was allocated for the stack of GetByte2 was not overwritten.
Thank's Vlad. That sounds logical. I couldn't find out why it worked. It shouldn't work according to the book :-)
Ok, this is not okey cause memory allocated on the stack of GetByte2 is overwritten.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char* GetByte2(char* ptr)
{
  char temp[100];
  printf("Byte2: "); 
  scanf("%s99", temp); 
  ptr = &temp[0];
  return ptr;
}


int main(int argc, char *argv[])
{
  char byte2[100]; 
  char* p = byte2;
  p = GetByte2(p);   printf("Byte2: %s\n", p);
 }



How come that this is ok then? I still am returning a value? Why doesn't the temp varable get out of scope? Is it the same reson i.e undefined behavior?

1
2
3
4
5
6
7
8
9
10
11
12
int GetVar()
{
  int temp = 5;
  return temp;    
}

int main()
{
  int var;
  var = GetVar();
  printf("GetVar: %d\n", var);  
}
The second example is valid. When you return temp a copy of it is created in stack. Then in the statement var = GetVar(); this copy is assigned to var. So there is no need to bother about temp that it will be destroyed because we already assigned it value to var.

The same is valid and for variable p from the first example, that is p has valid value. But in the first example we are trying to access the array in the stack of other function not the pointer p itself.
Last edited on
Yes you are right. The function GetVar returns the value 5.
This function works fine (comparing to the function GetByte trying to return
address not the value).
The function GetVar returns 5 not the address of temp.
In function GetByte2 the return value is the address of the array temp[0].
But this address is no longer valid because the temp-array is out
of scope and the memory area is released.

/Jen
You can use the static keyword to have some fun without fear.
A static variable behaves like a global, in that it doesn't go out of scope, and like a local, in that its scope is not global. I think.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

char * valid()
{
	static char t[] = "Well hello there.";

	return t; // needn't use &t[0]
}

int main()
{
	valid()[11] = 'J';
	valid()[12] = 'e';
	valid()[13] = 'n';
	valid()[14] = 'n';
	valid()[15] = 'y';

	std::cout << valid() << std::endl;
}


Edit: oops, seems I missed dadabe's post. Oh well.
Last edited on
Topic archived. No new replies allowed.