473,322 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Initializing structure member( char array)

I have declared an char array in my structure.After creating instance of the structure,i initialized its members in following way.

structure student
{
char name[20];
int rol_no;
}

int main()
{
student s1;
s1.name = "jimmy";
// some more statements..
}

this code gives me compilation error as "lvalue required before '=' operator "

Can anyone please explain me why??
Sep 20 '06 #1
2 5899
As soon as you declare the variable of the structure, it takes memory. So it will take the memory for the array s1.name and the base address of this array get stored in s1.name.

Now the statement

s1.name = "jimmy";

is trying to change the base address of the array which is not possible.

Instead you use the strcpy(s1.name,"jimmy").

Let's analyse the following code.

char s[10];
s="jimmy";

Here s is an array of 10 characters. So in 10 bytes of memory will be allocated for storing the characters and we know that the base address will be stored in s.

Let us suppose that the base adress of the array be 100 and so it is get stored in s.

But when you print the size of the array it shows that 10 bytes have been occupied by this array. Then where did the s get stored, i.e., the base address 100 get stored.

So it means that it does not take any space for storing the base address of the array and the statement

s="jimmy";

is trying to change the base address of the array.
Sep 20 '06 #2
Thanks for ur reply..
Sep 20 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Steven T. Hatton | last post by:
Is there a way to initialize an member array of user defined objects that is declared static const? For example: const vec3f I(1,0,0); const vec3f j(0,1,0); class corner_functor {
2
by: Neno | last post by:
Hi, I have a linkage error that has something to do with the use of a static member array and I can't understand how to solve the problem. Can someone help me please? In detail: A class Month...
21
by: jimmy | last post by:
Hi, I would like to initialize a member array of 3 floats with a memcpy (for reasons of efficiency). Later I would like to access the floats via public members (e.g. foo.x, foo.y, foo.z). Is...
3
by: Kaz Kylheku | last post by:
Given some class C with array T x, is it possible to get a pointer-to-data-member to one of the elements? &C::x gives us a pointer-to-member-array: T (C::*). But I just want to get a T C::*...
4
by: Chang Byun | last post by:
Hi, folks, I have a question about structure array as a argument. The below short program is that main function call a subroutine which increases real and imaginary part by 1 respectively in...
12
by: anonymous | last post by:
Hi folks, I am in a fix trying to copy data to an array which is member of a structure. What I am doing right now is: char array = {0,1,2,3,4,5,6,7}; memcpy(structure.array, array, 8); Is...
2
by: Charles Law | last post by:
Does anyone know if it is possible to initialise a structure array at run-time, something like this: <code> Structure struct Dim a As String Dim b As Integer Dim c As End Structure
0
by: JoeyB | last post by:
Hey All, My app will have a structure array that will hold the parameters used to initialize a array of classes at run time to default values. XML looks like a good candidate to persist the data...
7
by: runn | last post by:
Can a class has a member array which will be dynamically allocated during runtime? Such as double* A=new double here n is an integer member variable and will be determined during runtime and...
2
by: Peng Yu | last post by:
Hi, I'm wondering if there is a way to initialized a member array just as the initialization of a member variable? Or I have to initialized the array inside the function body of the constructor?...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.