Archived

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

PumpkinPieman

Char Arrays and Pointers

Recommended Posts

I''ve been having a hard time with this, I understand objects and stuff, just not character pointers to character arrays
// VName.h: interface for the CVName class.

//

//////////////////////////////////////////////////////////////////////


#if !defined(AFX_VNAME_H__F22CD424_BBA3_4300_A112_3EC221A220B3__INCLUDED_)
#define AFX_VNAME_H__F22CD424_BBA3_4300_A112_3EC221A220B3__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


#include <string.h>

class CVName  
{
public:
	void setName(const char m_pName);
	char* getName();
	CVName();
	virtual ~CVName();

private:
	char* name;
};

#endif // !defined(AFX_VNAME_H__F22CD424_BBA3_4300_A112_3EC221A220B3__INCLUDED_)

// VName.cpp: implementation of the CVName class.

//

//////////////////////////////////////////////////////////////////////


#include "VName.h"

//////////////////////////////////////////////////////////////////////

// Construction/Destruction

//////////////////////////////////////////////////////////////////////


CVName::CVName()
{
	name = new char[21];
}

CVName::~CVName()
{

}


char* CVName::getName()
{
	return name;
}

void CVName::setName(const char m_pName)
{
	strncpy(name, &m_pName, 20);
}
How do I pass more then one char through a parameter?

Share this post


Link to post
Share on other sites
pass a character pointer
a string in C/C++ (to my knowledge) is a pointer to an array of characters. the last character is the null character (which is how you can test the length of strings and stuff).
in your code, just change
void setName(const char m_pName);
to
void setName(const char* m_pName);
now it is a character pointer (a string)
you can pass it around just like any other pointer.

Share this post


Link to post
Share on other sites
first of all using "m_pName" as the argument name and "name" as the class member name, is bacwards.

you should put the "m_pName" in the class and "name" as the parameter. and to pass more then one char to a function do

function(char* name) or function(char name[])

but you could use the string class and use

function(string name)





[edited by - NumberXaero on October 8, 2003 12:59:56 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by PumpkinPieman
but if I use char* var as a parameter then I have to supply it with an address, what I want to do it sent it a character array. Or will it matter if I just send it a const or not?


If you''re looking to pass in an array then just pass in the array name. Don''t forget, arrays are just pointers who has a pointee that''s a first element of an array.





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
That works, thanks!
One more question:

void CVName::setName(char* m_pName)
{
strncpy(m_Name, m_pName, 20);
}


If I send the function greater then 18 char''s and print the string out there won''t be a \n so it will continue. How do I stop this?

Share this post


Link to post
Share on other sites
Well, first off you are using new without deleting it... second, you could simply allocate the proper amount of space for the string before copying it using strlen (or writing your own strlen which isn''t hard).


class String_C
{
private:
char *Str;
public:
String_C(void)
{
Str=0;
}
String_C(const char *Name)
{
Str=0;
SetName(Name);
}
SetName(const char *Name)
{
int tmp;
if (Str) //If somethings here already, delete it!

delete[] Str;
tmp = strlen(Name);
Str = new char[tmp+1]; //String + NULL

strncpy(Str,Name,tmp); //Copy the string

}
char *GetName(void)
{
return Str;
}
~String_C(void)
{
if (Str)
delete[] Str;
}
};

Share this post


Link to post
Share on other sites
When I add the stuff you have done it works fine, but there is one line that I found when going through debug that allocates more then it needs.


void CVName::setName(char* m_pName)
{
int tmp;
if (m_Name) //If somethings here already, delete it!

delete[] m_Name;

tmp = strlen(m_pName);
m_Name = new char[tmp + 1]; //String + NULL

strncpy(m_Name,m_pName,tmp); //Copy the string

}


The line:
m_Name = new char[tmp + 1]; //String + NULL
for some reason when it's passed 8 by tmp it doesn't create a 9 char array, or atleast it seems like it goes way past 9 like it never made a terminating null

[edited by - PumpkinPieman on October 8, 2003 5:48:46 PM]

Share this post


Link to post
Share on other sites
First thing's first, use std::string. Secondly, bear in mind that conversions to char* are are deprecated, as stated by the standard.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on October 9, 2003 10:56:40 AM]

Share this post


Link to post
Share on other sites
I''m kinda unfamiliar with strings, so I''ve been trying to find std::string on msdn (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang98/html/STRING2_string.asp) . How much more overhead does std::string use, and do I need it if I''m doing a class as simple as this?

I''m not going to use the string anymore then those 2 functions get\set.

Share this post


Link to post
Share on other sites