getting the error while passesing the pointer from the function

HI,
i am getting the error while displaying the char array .
Not able to understand what am i doing wrong .
any help appreaciated .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std; 

char getbuffer();



int _tmain(int argc, _TCHAR* argv[])
{
	char name[30] ;
   name = getbuffer();
	//strcpy( name, getbuffer() );
	cout <<name;

	return 0;
}
char getbuffer()
{
	char buffer[] = "Captain <>";
	return (*buffer);
}


and thanks in advance .
I think the errors are because of

char buffer[] = "Captain <>";

It's because your character array (buffer) is being initialized as if it was a string. I just tried to run this bit of code:

1
2
3
4
int main(){
    char charArray[] = "hello World";
    cout << charArray;
}


but I got errors because the array was initialized incorrectly.

what you might want to do is change buffer into a string.
Last edited on
Your function returns a char (it returns the char 'C', and just that char - it does not return anything else and when the function finishes, the array you made here char buffer[] = "Captain <>"; effectively ceases to exist - trying to use it is wrong).

Anyway, back to your function. It returns a char. You then try to make the object name (which is effectively a char-pointer) equal to that char. You cannot make a char-pointer equal to a char.

You do not understand the following things:

* Pointers
* Local variables

Please read this. If you still don't understand pointers, come back and ask more.
http://www.cplusplus.com/articles/EN3hAqkS/

It seems a little odd, as you've been here for a while. Do you generally avoid pointers?
Last edited on
that was, I think, a little harsh, moschops. bluecoder, in your getbuffer function, you might want to return the memory address of buffer rather than a pointer.

return &buffer;
return &buffer; return buffer; I think you meant? Returning &buffer will return the address of a pointer that itself contains the address of the 'C'.

return the memory address of buffer rather than a pointer.

A pointer is a memory address (or NULL). The only way to return a memory address without using a pointer is by casting it into some other form (such as an int), and changing it back again afterwards, which is possible but pointless.

Anyway, that will make it compile, but since at the end of the function, all the local variables are destroyed, the code will be accessing memory that might contain the data and might contain whatever else has been written over it. This is a bigger conceptual issue than just making sure to return a pointer :) Returning a pointer to the array does not return the array; the array will die when that function ends.
Last edited on
ah.. I see. Your a good teacher moschops. haha thnx
Thanks Moschops , and Bufflez ..
yes , i can't run form pointer , ( i try to avoid it as far as possible ) , i have to get over it ,
thanks again

Moschops !!

Thanks again you both ..
I have solved it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std; 

char* getbuffer();



int _tmain(int argc, _TCHAR* argv[])
{
	char name[30] ;
  // name = getbuffer();
	strcpy( name, getbuffer() );
	cout <<name;

	return 0;
}
char* getbuffer()
{
	char buffer[] = "Captain <>";
	return (buffer);
}
Your solution is still bad. You are relying on a section of memory holding Captain <> not to be disturbed, even though it is free for reuse. You are copying it as fast as you can in an attempt to make sure that you get it before it is written over ( strcpy( name, getbuffer() ); ) but that's just tilting the odds in your favour. Why not make something that doesn't rely on luck to work? For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace std; 

char* getbuffer();



int main()
{
	char name[30] ;
  	cout <<name;

	return 0;
}
void fillBuffer(char* bufferToFill)
{
	char buffer[] = "Captain <>";
        strcpy( name, buffer); // Now you're copying from memory that isn't free for reuse and my have been trashed
}

Thanks again Moschops

I got you .. you are right ..

the code that worked now is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
using namespace std; 

void fillBuffer(char* bufferToFill);



int main()
{
	char name[30] ;
	fillBuffer(name);
  	cout <<name;

	return 0;
}
void fillBuffer(char* bufferToFill)
{
	char buffer[] = "Captain <>";
        strcpy( bufferToFill, buffer); // Now you're copying from memory that isn't free for reuse and may have been trashed
}


thanks again
Last edited on
Topic archived. No new replies allowed.