Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

charcter arrays

Posted on 2000-04-17
14
Medium Priority
?
214 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
Industry Leaders: 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 60 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

705 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