Solved

charcter arrays

Posted on 2000-04-17
14
211 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

820 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