Solved

charcter arrays

Posted on 2000-04-17
14
212 Views
Last Modified: 2010-05-18
How do I store data in a character array.  

I got
char fEfirstName[10];
char fElastName[10];
char sEfirstName[10];
char sElastName[10];

I guess the question would be how would I store the employee's name in an array.  


0
Comment
Question by:nationnon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +3
14 Comments
 
LVL 3

Expert Comment

by:MDarling
ID: 2722869
use strcpy

for example...


strcpy(fEFirstName,"Michael");

0
 
LVL 3

Expert Comment

by:MDarling
ID: 2722879
be especially careful about overruning the end of your arrays though.

for instance this would be bad...

char name[5];

strcpy(name,"Michael");

this would overwrite the end of the array and would cause some data corruption.

what I'm saying is ensure that your arrays are large enough to hold the data you wish to put in them.

you should also test that they are!

regards,
Mike.


0
 
LVL 22

Expert Comment

by:nietod
ID: 2722923
In C++ it is often best to avoid using character arrays directly, because of the mistakes MDarling pointed out.   Instead you should consider using a string class.  These tend to be more efficient and are much much easier and safer to use.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:Shay050799
ID: 2723187
nietod is right
if u are using MFC/STL use thir own string classes, that assure u to avoid memory leak/memory overwritten.
well most of the times.

be very carefull with CString get buffer though.

feel free to ask more questions if u need.

what i would do is this
char m_szFirstName[64];
memcpy(m_szFirstName,"michael",7);

Shay
0
 
LVL 3

Expert Comment

by:MDarling
ID: 2723224
If you did use memcpy, Shay, you would need to add the null on the end yourself.

how about this...

int CopyFirstName(char *szName)
{
  if(strlen(szName)>=sizeof(fEFirstName))
    return -1; // string is too big - error

  strcpy(fEFirstName,szName);
}

regards,
Mike.
0
 
LVL 1

Expert Comment

by:MT_MU
ID: 2723229
I think what you are actually asking about is mutlti-dimensional arrays.

Re your example...


char Names[20][10];  // reserve space for 20 names 10 characters long.

then to populate these names..

strcpy(Names[whichone],"Frank");

Other alternatives (since you are using first/last name combinations).

Create a structure...

typedef struct tag_MyNameStruct
{
      char FirstName[10];
      char LastName[10];
} MyNameStruct;

MyNameStruct Names[20];

Then you could use
strcpy(Names[index].FirstName,"Frank");

When using arrays you do have to be concerned about array bounds.  (Ie you don't want to write to memory that you don't own - arrays don't have any built in checks for this.)


0
 
LVL 3

Expert Comment

by:MDarling
ID: 2723243
if you did use memcpy, Shay, you would need to add the null yourself.

how about this...

int CopyFirstName(char *Name)
{
  if(strlen(Name)>=sizeof(fEFirstName))
    return -1; // error - name too big

  strcpy(fEFirstName,Name);

  return 0; // success
}



regards,
Mike.
0
 
LVL 3

Expert Comment

by:Shay050799
ID: 2723292
mike u are right i didn't think about the NULL its absolutly valid comment
but about ur sizeof(...) what if he use different pragma ?
then the size is different isn't it ??

Shay
0
 
LVL 1

Expert Comment

by:MT_MU
ID: 2723332
There is no pragma that makes any difference on the space allocated to indivual arrays.

You are think of #pragma pack() - which according to the doc..."Nonstatic class-member data is stored in such a way that items falling between access specifiers are stored at successively higher memory addresses".

This prevent the compiler from padding structs/classes to ensure things end up on "even" boundarys



0
 
LVL 3

Expert Comment

by:MDarling
ID: 2723383
There are no end of ways to ensure the safety of the arrays.  We just have to ensure we don't get lazy and not implement one of them.  

Using a string class like nietod said is probably the best way if you can do that.  

However a lot of programmers don't have access to (or want to use for space reasons) the class libraries.  Some of them might even be students and not allowed to use them :)




0
 
LVL 22

Expert Comment

by:nietod
ID: 2723425
>> memcpy(m_szFirstName,"michael",7);

>> If you did use memcpy, Shay, you would
>> need to add the null on the end yourself
Well there os a NUL at the end of the string literal, so you just have to be sure to copy it.  Since "michael" is 7 bytes, if you copied 8 you would be fine.  i.e it would give the same results as strpy().
0
 
LVL 1

Accepted Solution

by:
Vinay earned 20 total points
ID: 2727494
Hi friend,
scanf("%s",fEfirstName);
scanf("%s",fElastName);
scanf("%s",sEfirstName);
scanf("%s",sElastName);

i fell this is the simplest way to scan input fromuser if u want to initialise

char fEfirstName[10]={"Vinay"};
char fElastName[10]={"Samant"};
char sEfirstName[10]={"Apeksha"};
char sElastName[10]{"Samant"};

otherwise all other ideas specified by strcpy will also work but it increases the code unnecessarily ..... but i also fell it is better to ensure whether the length of input is compitable to the length of array or not & for that u can always strlen function.
Take care,
Vinay



0
 
LVL 1

Author Comment

by:nationnon
ID: 2739784
Multi dimensional arrays , how would they be benificial with what i'm trying to do and how would it work?  Give me some visionary examples.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2742568
>> how would they be benificial with what i'm
>> trying to do and how would it work?
The problem is we don't know what you are trying to do.

If you could explain what it is you need to do, we could provide better advice and more details.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question