Solved

charcter arrays

Posted on 2000-04-17
14
208 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
Comment Utility
use strcpy

for example...


strcpy(fEFirstName,"Michael");

0
 
LVL 3

Expert Comment

by:MDarling
Comment Utility
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
Comment Utility
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
 
LVL 3

Expert Comment

by:Shay050799
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 3

Expert Comment

by:Shay050799
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now