Some pointer problems

Grab your favourite IDE and tinker with the innards of game engines

Some pointer problems

Postby Garrador on Sat Feb 05, 2011 12:03 pm

Ok, this is the code, and how I want it to work:
Code: Select all
const char *m_pszFirstName;
const char *m_pszMiddleName;
const char *m_pszLastName;
const char *m_pszFullName;

const char *GetFullName()
{
   // TODO: Need to get First, middle and last name
   //            into pszFullName
   return m_pszFullName;
}


There is a draft of the class. My problem lies here.
I need to get the first, middle and last name into fullname. It's obviously not possible to add them like this:
fullname = first + middle + last; since it's adresses, but how would I go about "loading" the fullname pointer with the adresses of the other three so it makes out the whole name in one pointer?

Hoping for swift answers! =)
Gar
You click on Build or type
make (or some equivalent), and you are astonished, then mortified, as you realize that the whole world is being
recompiled and relinked!
- Scott Meyers
User avatar
Garrador
Veteran
Veteran
 
Joined: Fri May 12, 2006 10:39 pm
Location: Norway

Re: Some pointer problems

Postby zombie@computer on Sat Feb 05, 2011 7:34 pm

Garrador wrote:Ok, this is the code, and how I want it to work:
Code: Select all
const char *m_pszFirstName;
const char *m_pszMiddleName;
const char *m_pszLastName;
const char *m_pszFullName;

const char *GetFullName()
{
   // TODO: Need to get First, middle and last name
   //            into pszFullName
   return m_pszFullName;
}


There is a draft of the class. My problem lies here.
I need to get the first, middle and last name into fullname. It's obviously not possible to add them like this:
fullname = first + middle + last; since it's adresses, but how would I go about "loading" the fullname pointer with the adresses of the other three so it makes out the whole name in one pointer?

Hoping for swift answers! =)
Gar
theres two options. The easy way out and the long way around

easy
Code: Select all
const char *GetFullName()
{
   static char buff[1024];//some buffer. Length has to be greater than any combo of first, middle and last name.
   strcpy(buff, m_pszFirstName);//copy
   strcat(buff, m_pszMiddleName);//concatenate
   strcat(buff, m_pszLastName); //concatenate
   return &buff; //return address of buffer
}

int main()
{
  std::cout << GetFullName();
  //note: getfullname() uses a static buffer. Multiple calls returning different names is possible, but since there is only one buffer, it will always contain the last generated name
  return 0;
}




long way around

Code: Select all
char *GetFullName()
{
   char *buff = new char[ strlen(m_pszFirstName) + strlen(m_pszMiddleName) + strlen(m_pszLastName) +1];//create buffer thats exactly large enough (lenghts of all strings you want to put into it, plus one for the string-terminator '\0')
   strcpy(buff, m_pszFirstName);//copy
   strcat(buff, m_pszMiddleName);//concatenate
   strcat(buff, m_pszLastName); //concatenate
   return buff; //return address of buffer
}

int main()
{
    char *name = GetFullName();
   //use name here
   //when you are done with name (DONT FORGET, WILL CAUSE MEMORY LEAKS)
   delete [] name;
}

offcourse, if you are using c++, you can also use strings...
When you are up to your neck in shit, keep your head up high
zombie@computer
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Fri Dec 31, 2004 5:58 pm
Location: Lent, Netherlands

Re: Some pointer problems

Postby Garrador on Sat Feb 05, 2011 8:50 pm

Ah great! Thanks a lot! Worked like a charm!

Thanks
You click on Build or type
make (or some equivalent), and you are astonished, then mortified, as you realize that the whole world is being
recompiled and relinked!
- Scott Meyers
User avatar
Garrador
Veteran
Veteran
 
Joined: Fri May 12, 2006 10:39 pm
Location: Norway

Re: Some pointer problems

Postby Shrinker on Sat Mar 05, 2011 10:04 pm

Use std::string!
User avatar
Shrinker
Been Here A While
Been Here A While
 
Joined: Fri Nov 09, 2007 5:52 pm
Location: Germany

Re: Some pointer problems

Postby Garrador on Tue Mar 08, 2011 1:09 pm

Like I said, I wanted to do it with the char type, so I know how to do it :P Strings are easy, heh.
You click on Build or type
make (or some equivalent), and you are astonished, then mortified, as you realize that the whole world is being
recompiled and relinked!
- Scott Meyers
User avatar
Garrador
Veteran
Veteran
 
Joined: Fri May 12, 2006 10:39 pm
Location: Norway

Return to Programming

Who is online

Users browsing this forum: No registered users