Solved

charcter arrays

Posted on 2000-04-17
14
210 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

786 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