-4

I have the below array,

struct Student{
char* Name;
int age;
Student* Next;
};

And i used to follow the below steps to access the char* fields.

Method 1;

Student s1 = {strdup("Name1"),26};

Method2:

Student s2;
s2.Name = strdup("Name2");
s2.age  = 26;

Here i wanted to know is there any other way to access the char* Field and if not is there a specific and best way to access char* fields?.

Also what is the best way to have a datastructure like name, address and etc.. inside a struct. should i go with the above method char*? or can i have array instead? or string will make sense in this?(is there any other method is available instead of char*. char array[], string stringname )

My ultimate Aim is to have a data structure correctly with best access methodology. pls Help!!!

struct DataStruct{
(char*/char [] / string) Name; // Need a best way 
} 

also the access mechanism.

if we have only three mechanisms like char* , char [] and string, then please advise the best way of accessing them from int main().

Many Thanks

JeJo
  • 30,635
  • 6
  • 49
  • 88
  • 5
    if you are dealing with C++, `std::string` the best! – JeJo May 27 '18 at 12:01
  • 3
    Define "best". [[[[[[[[ – Cheers and hth. - Alf May 27 '18 at 12:03
  • 1
    if you are in c++, use classes, std::string and std::vector – skeller May 27 '18 at 12:05
  • Yes !, my code is on C++. thank you for the replay. may i have some more question on top of this – user2285375 May 27 '18 at 12:05
  • 4
    There is no language called C/C++ - pick one of the two – UnholySheep May 27 '18 at 12:05
  • @Cheersandhth.-Alf Hi i wanted to have a feasible datastructure , i am requesting : what is the best way to achieve that. – user2285375 May 27 '18 at 12:09
  • @UnholySheep Please consider in C++ way!. sorry for the confusion – user2285375 May 27 '18 at 12:10
  • hi @JeJo thanks for your reply, could you tell me why i should not use array or char*. any performance issues? pls descibe – user2285375 May 27 '18 at 12:12
  • @user2285375: I don't wanna repeat: https://stackoverflow.com/questions/10937767/when-to-use-stdstring-vs-char?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa and https://stackoverflow.com/questions/801209/char-vs-stdstring-in-c – JeJo May 27 '18 at 12:21
  • C style string introduce an unnecessary "layer of pointer" (in terms of direct use). Also they have the inconvenience of being null terminated. `std::string` abstracts this away in an handy fashion. – Vivick May 27 '18 at 12:23
  • Also, `char*` and `char[]` can be considered equivalent – Vivick May 27 '18 at 12:23
  • thanks @Vivick , I would like to know with respect to accessing the char[] in my code. struct{ char name[25]; }; int main() { //accessing the name in the struct } pls help – user2285375 May 27 '18 at 12:28
  • Let's say you have an instance of said struct named `s`, do `char[25] name = s.Name;` – Vivick May 27 '18 at 12:30
  • //Data Structure struct NameStruct{ char Name[10]; }; int main() { NameStruct NM1; NM1.Name = "Name"; NameStruct NM2={"Name2"}; } Could you tell me what's correct and wrong with the above example. why i'm not able to access array element by NM1.Name = "Name"; what is wrong. And How i am able to access by initializing the object(thru constrctor - obj2 in the above example) @Vivick please comment – user2285375 May 27 '18 at 12:42
  • also what is it happening with respect to object initialisation. – user2285375 May 27 '18 at 12:43
  • Alright got it, I'll propose an answer – Vivick May 27 '18 at 12:45
  • Thanks a lot @Vivick – user2285375 May 27 '18 at 12:48

1 Answers1

0

As for what to choose between char*/char[] and std::string, as I said in a comment :

C style strings introduce an unnecessary "layer of pointer" (in terms of direct use). Also they have the inconvenience of being null terminated. std::string abstracts this away in an handy fashion

Regarding the different initialization approaches : Both are nearly equivalent as is.

This means that without any kind of transformation,

Student s1 = {strdup("Name1"),26};

and

Student s2;
s2.Name = strdup("Name2");
s2.age  = 26;

Are interchangeable.

The difference lies elsewhere :

Student s1 = {strdup("Name1"),26};
cout << s1.Name;

Student s2;
cout << s2.Name;
s2.Name = strdup("Name2");
s2.age  = 26;

Here, there is what we call Undefined Behavior. You are accessing s2.Name before it is initialized/given a value.

If you have to choose, prefer Student s1 = {strdup("Name1"),26}; which guarantees that any use of the members is valid. If you need (in some scenario) the second approach then be certain that what you are accessing is actually initialized.

Vivick
  • 3,434
  • 2
  • 12
  • 25