Why my below statement throw error ?
-
I have question regarding below statement
record[0].name[30] ="mangal";
when i use it in below code it throw error. i want to know technical reason behind that .
#include <stdio.h> #include <string.h> struct student{ int id; char name[30]; float percentage; }; int main(){ int i; struct student record[2]; // 1st student's record record[0].id=1; record[0].name[30] ="mangal"; // strcpy(record[0].name, "Bhanu"); -
I have question regarding below statement
record[0].name[30] ="mangal";
when i use it in below code it throw error. i want to know technical reason behind that .
#include <stdio.h> #include <string.h> struct student{ int id; char name[30]; float percentage; }; int main(){ int i; struct student record[2]; // 1st student's record record[0].id=1; record[0].name[30] ="mangal"; // strcpy(record[0].name, "Bhanu");@Qt-embedded-developer said in Why my below statement throw error ?:
when i use it in below code it throw error. i want to know technical reason behind that .
Seriously? Do you think posting the error message you are receiving from the compiler would be relevant, or should we guess?
record[0].name[30] ="mangal";The most basic C/C++. What type is
record[0].name[30]? I'm quite sure whatever error message you are receiving tells you this.... -
@Qt-embedded-developer said in Why my below statement throw error ?:
when i use it in below code it throw error. i want to know technical reason behind that .
Seriously? Do you think posting the error message you are receiving from the compiler would be relevant, or should we guess?
record[0].name[30] ="mangal";The most basic C/C++. What type is
record[0].name[30]? I'm quite sure whatever error message you are receiving tells you this....@JonB error is telling me assignment is not possible. But i not know why ?
-
@JonB error is telling me assignment is not possible. But i not know why ?
@Qt-embedded-developer PLEASE post the whole error!
And PLEASE also answer @JonB question!
Help others to help you, do not make it unnecessary difficult to help you! -
@JonB error is telling me assignment is not possible. But i not know why ?
@Qt-embedded-developer
Your error might be this:
error: array type 'char [30]' is not assignableYou just can't do "record[0].name[30] ="mangal";"
As the contents are modifiable, the arrays themselves are not.
Use strcpy from <string.h> instead.#include <stdio.h> #include <string.h> #include <iostream> struct student{ int id; char name[30]; float percentage; }; int main(){ int i; struct student record[2]; // 1st student's record record[0].id=1; strcpy(record[0].name,"mangal"); // 2st student's record record[1].id=2; strcpy(record[1].name,"himanshu"); //test std::cout << record[0].name << std::endl; std::cout << record[1].name << std::endl; }Just one hint - I would do the "record = id" ;-) its easier to handle.
-
@JonB error is telling me assignment is not possible. But i not know why ?
@Qt-embedded-developer Why don't you use QString instead of char array?
@lacuna explained you why you get that error (you need to understand that you can't assign to an array like you do). -
@JonB error is telling me assignment is not possible. But i not know why ?
@Qt-embedded-developer said in Why my below statement throw error ?:
@JonB error is telling me assignment is not possible. But i not know why ?
For goodness sake, that is not an error message.... Really how do you expect people to help you?
-
@JonB error is telling me assignment is not possible. But i not know why ?
@Qt-embedded-developer said in Why my below statement throw error ?:
But i not know why ?
Are you understanding what you are coding?
You have defined a struct which constains an int value calledid, a float value callpercentageand an array of char callednamewhich have 30 elements.And in your code you are trying update item 31 of
nameand you are surprise that this do not work!!Are you serious?
-
@Qt-embedded-developer said in Why my below statement throw error ?:
But i not know why ?
Are you understanding what you are coding?
You have defined a struct which constains an int value calledid, a float value callpercentageand an array of char callednamewhich have 30 elements.And in your code you are trying update item 31 of
nameand you are surprise that this do not work!!Are you serious?
@KroMignon said in Why my below statement throw error ?:
And in your code you are trying update item 31 of name and you are surprise that this do not work!!
It's not that which I was drawing attention to. I am expecting OP to get a compiler error message:
struct student{ char name[30]; } record[0].name[30] ="mangal";The type of
name[30]versus the type of"...". -
@KroMignon said in Why my below statement throw error ?:
And in your code you are trying update item 31 of name and you are surprise that this do not work!!
It's not that which I was drawing attention to. I am expecting OP to get a compiler error message:
struct student{ char name[30]; } record[0].name[30] ="mangal";The type of
name[30]versus the type of"...".@JonB said in Why my below statement throw error ?:
It's not that which I was drawing attention to. I am expecting OP to get a compiler error message:
Yes, I also saw this, but this is one of many error in this code extract:
- index out of bounds
- bad types
- wrong assignment statement
- ...
Impressive how many errors can be done in less than 100 lines of code :D