Archived

This topic is now archived and is closed to further replies.

Krypto night

Pointer Problem

Recommended Posts

Krypto night    122
I have a problem with pointers and arrays. Im using c++ and VC++ 6.0
#include <iostream>
#include <string>

using namespace std;

int convert(char ** dave, int charcount);
char deconvert(int dave);
void display(string name);
string returnname();

int main()
{
	string name;
	int asciiname;
	int charcount;
	int i;
	
	name = returnname();
	
	charcount = name.size();
    
	char * chararray = new char[charcount];

	for( i = 0; i < charcount; i++)
	{
		chararray[i] = name.at(i);
		cout << chararray[i] << endl;
	}

	asciiname = convert(&chararray, charcount);

	return 0;

}


string returnname()
{
	string name;
	cout << "What is your name? >> ";
	getline(cin, name);

	return name;
}

int convert(char ** dave, int charcount)
{
	int i;
	int a = charcount;
		
    int * intarr = new int[charcount];

	for (i = 0; i < charcount; i++)
	{
		intarr[i] = (int) dave[i];
	}

	return ** &intarr;
		

}
The code does work but i dont understand why. Its this line that confuses me.
return ** &intarr;
i dont understand this and i only arived at the soloution through trial and error. Can any one help me to understand this?

Share this post


Link to post
Share on other sites
MadKiwi    122
Well I''m only a newbie myself but isn''t:

return ** &intarr;

the same as:

return * intarr;

ie. you are returning the value your pointer points to.

Share this post


Link to post
Share on other sites
Zahlman    1682
It doesn''t do what you want, anyway (unless all you "want" to do is try and figure out a couple of things about how pointers work). You''re creating an array of integers, and then just returning the first one of those; the array is now inaccessible when convert() returns, and is a memory leak.

The innermost * and & should cancel out, so that "** &intarr" is the same as "* intarr". I.e., you just dereference your "int * intarr" variable, resulting in a value of type int. The returned value is thus the integer that ''intarr'' was pointing at, which is the first integer in the array (element 0).

Your names suggest that you''re thinking of ASCII values as integers rather than ''chars''. Clue moment: "char" is C/C++ -ese for "byte". IT IS NO LONGER THE CASE THAT IT HAS ANYTHING TO DO WITH A TEXT CHARACTER, unless your language is ASCII *and* you are doing lower-level work than you probably need to. Witness unicode, wide chars etc. Java pretended to fix the problem by making "char" a 2-byte unsigned type and using "byte" to refer to the old char - but Unicode allows for "code points" above 65535 so this isn''t always enough space, and it still causes the problem of confusing a "printable character type" with a "numeric value type".

Anyway. You''re manually extracting a char array from the std::string; this is work you don''t need to do - see std::string.c_str(). (More knowledgeable people will be able to tell you how std::string handles Unicode.) You want a return type of int * on the convert() function, and just return intarr; if you''re going to do things this way (more knowledgeable people will probably be able to tell you how to do it more nicely, with references) - but unless you''re just tooling around, I can''t really see why you need to do such a conversion at all.

Share this post


Link to post
Share on other sites
Krypto night    122
Yup turns out it is...
I felt sure i had tried that but turns out i didnt. Another question i need to ask is about the;

int convert(char ** dave, int charcount)


Why do i need two ** ? Seems strange to me but who am i to argue?

Zahlman:
So how do i pass back the entire array? I know that my knoledge on things like that is not brilliant but the point is that the code isnt meant to do anything. Im just doing it to learn about dynamic memory and passing arrays to and from functions.

[edited by - krypto night on March 29, 2004 5:34:17 PM]

[edited by - krypto night on March 29, 2004 5:34:41 PM]

Share this post


Link to post
Share on other sites
Zahlman    1682
You pass back the array by using "int *" as your return type and "intarr" as your return value. You don''t really pass back "an array", but rather a pointer to the memory where the array resides. This is ok as long as your calling code knows how much data is available there - an array is basically implemented using a const pointer to its beginning.

One warning here. This works in your case because you are allocating the ''intarr'' using new[]. I.e., your data gets put on the heap. If you made an int[] in your convert() instead of an int*, then that would be a stack-allocated (non-dynamic) piece of memory - and that''s Bad News. When you exit your function, that memory is now sitting on your stack where it is an easy target to get overwritten by the calling code - if you''re lucky. (If not, that memory has already been written over due to some internal optimization, or even free()''d, or who knows what). So now you have a pointer to memory that is *not valid*, and Bang You''re Dead.

Share this post


Link to post
Share on other sites