How to assign a structure member to a variable in C












0















Hi everyone i'm new in C and i want to know if it's possible to assign a structure member to a variable in C like below



struct User
{
int ID;
char name[10];

}users[10];

int uid,
char userName;

uid = users[i].ID;
userName = user[i].name;


trying to do something like above i got a warning that "assignment makes integer from pointer without a cast"
and when compile i run since it was a warning not an error it run but then i can't print the value of uid or userName the system will just quit;
Please i know many will ask while can't i just access the structure member directly and print it i know i can do it that way but what i actually want to to do is to see a way i can assign the value to variable










share|improve this question

























  • Without seeing a Minimal, Complete, and Verifiable example I can only guess that user[i].name is a pointer (i.e. char * )? A char is not the same as a char *.

    – Some programmer dude
    Dec 30 '18 at 14:46











  • well the question has very less details. but change the char userName to char *userName if the user[i].name is a string.

    – Revolver
    Dec 30 '18 at 14:51













  • @ Revolver thanks for helping i will edit my question and post the structure

    – sam
    Dec 30 '18 at 14:56











  • A name cannot be a single character. Member name is either a char * or a char. In both cases you cannot assign to a variable of type char.

    – Gerhardh
    Dec 30 '18 at 14:58











  • Thanks to everyone your suggestion work right i define the variable name as a pointer and that solve the problem

    – sam
    Dec 30 '18 at 15:03
















0















Hi everyone i'm new in C and i want to know if it's possible to assign a structure member to a variable in C like below



struct User
{
int ID;
char name[10];

}users[10];

int uid,
char userName;

uid = users[i].ID;
userName = user[i].name;


trying to do something like above i got a warning that "assignment makes integer from pointer without a cast"
and when compile i run since it was a warning not an error it run but then i can't print the value of uid or userName the system will just quit;
Please i know many will ask while can't i just access the structure member directly and print it i know i can do it that way but what i actually want to to do is to see a way i can assign the value to variable










share|improve this question

























  • Without seeing a Minimal, Complete, and Verifiable example I can only guess that user[i].name is a pointer (i.e. char * )? A char is not the same as a char *.

    – Some programmer dude
    Dec 30 '18 at 14:46











  • well the question has very less details. but change the char userName to char *userName if the user[i].name is a string.

    – Revolver
    Dec 30 '18 at 14:51













  • @ Revolver thanks for helping i will edit my question and post the structure

    – sam
    Dec 30 '18 at 14:56











  • A name cannot be a single character. Member name is either a char * or a char. In both cases you cannot assign to a variable of type char.

    – Gerhardh
    Dec 30 '18 at 14:58











  • Thanks to everyone your suggestion work right i define the variable name as a pointer and that solve the problem

    – sam
    Dec 30 '18 at 15:03














0












0








0








Hi everyone i'm new in C and i want to know if it's possible to assign a structure member to a variable in C like below



struct User
{
int ID;
char name[10];

}users[10];

int uid,
char userName;

uid = users[i].ID;
userName = user[i].name;


trying to do something like above i got a warning that "assignment makes integer from pointer without a cast"
and when compile i run since it was a warning not an error it run but then i can't print the value of uid or userName the system will just quit;
Please i know many will ask while can't i just access the structure member directly and print it i know i can do it that way but what i actually want to to do is to see a way i can assign the value to variable










share|improve this question
















Hi everyone i'm new in C and i want to know if it's possible to assign a structure member to a variable in C like below



struct User
{
int ID;
char name[10];

}users[10];

int uid,
char userName;

uid = users[i].ID;
userName = user[i].name;


trying to do something like above i got a warning that "assignment makes integer from pointer without a cast"
and when compile i run since it was a warning not an error it run but then i can't print the value of uid or userName the system will just quit;
Please i know many will ask while can't i just access the structure member directly and print it i know i can do it that way but what i actually want to to do is to see a way i can assign the value to variable







c struct






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 30 '18 at 14:58







sam

















asked Dec 30 '18 at 14:43









samsam

3711621




3711621













  • Without seeing a Minimal, Complete, and Verifiable example I can only guess that user[i].name is a pointer (i.e. char * )? A char is not the same as a char *.

    – Some programmer dude
    Dec 30 '18 at 14:46











  • well the question has very less details. but change the char userName to char *userName if the user[i].name is a string.

    – Revolver
    Dec 30 '18 at 14:51













  • @ Revolver thanks for helping i will edit my question and post the structure

    – sam
    Dec 30 '18 at 14:56











  • A name cannot be a single character. Member name is either a char * or a char. In both cases you cannot assign to a variable of type char.

    – Gerhardh
    Dec 30 '18 at 14:58











  • Thanks to everyone your suggestion work right i define the variable name as a pointer and that solve the problem

    – sam
    Dec 30 '18 at 15:03



















  • Without seeing a Minimal, Complete, and Verifiable example I can only guess that user[i].name is a pointer (i.e. char * )? A char is not the same as a char *.

    – Some programmer dude
    Dec 30 '18 at 14:46











  • well the question has very less details. but change the char userName to char *userName if the user[i].name is a string.

    – Revolver
    Dec 30 '18 at 14:51













  • @ Revolver thanks for helping i will edit my question and post the structure

    – sam
    Dec 30 '18 at 14:56











  • A name cannot be a single character. Member name is either a char * or a char. In both cases you cannot assign to a variable of type char.

    – Gerhardh
    Dec 30 '18 at 14:58











  • Thanks to everyone your suggestion work right i define the variable name as a pointer and that solve the problem

    – sam
    Dec 30 '18 at 15:03

















Without seeing a Minimal, Complete, and Verifiable example I can only guess that user[i].name is a pointer (i.e. char * )? A char is not the same as a char *.

– Some programmer dude
Dec 30 '18 at 14:46





Without seeing a Minimal, Complete, and Verifiable example I can only guess that user[i].name is a pointer (i.e. char * )? A char is not the same as a char *.

– Some programmer dude
Dec 30 '18 at 14:46













well the question has very less details. but change the char userName to char *userName if the user[i].name is a string.

– Revolver
Dec 30 '18 at 14:51







well the question has very less details. but change the char userName to char *userName if the user[i].name is a string.

– Revolver
Dec 30 '18 at 14:51















@ Revolver thanks for helping i will edit my question and post the structure

– sam
Dec 30 '18 at 14:56





@ Revolver thanks for helping i will edit my question and post the structure

– sam
Dec 30 '18 at 14:56













A name cannot be a single character. Member name is either a char * or a char. In both cases you cannot assign to a variable of type char.

– Gerhardh
Dec 30 '18 at 14:58





A name cannot be a single character. Member name is either a char * or a char. In both cases you cannot assign to a variable of type char.

– Gerhardh
Dec 30 '18 at 14:58













Thanks to everyone your suggestion work right i define the variable name as a pointer and that solve the problem

– sam
Dec 30 '18 at 15:03





Thanks to everyone your suggestion work right i define the variable name as a pointer and that solve the problem

– sam
Dec 30 '18 at 15:03












1 Answer
1






active

oldest

votes


















0














The problematic statement is



userName = user[i].name;
| |
char type char array type


firstly userName is declared as character variable and a character variable can't holds char array i.e user[i].name.



Change this



char userName;


to



char userName[10];


and then copy using strcpy() or strncpy(). For e.g



strcpy(userName, user[i].name);





share|improve this answer





















  • 2





    Never use strncpy (unless you have a very good reason that you can explain when asked about it). strncpy is a time bomb waiting to blow up in your face at any time.

    – gnasher729
    Dec 30 '18 at 15:21











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53978554%2fhow-to-assign-a-structure-member-to-a-variable-in-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The problematic statement is



userName = user[i].name;
| |
char type char array type


firstly userName is declared as character variable and a character variable can't holds char array i.e user[i].name.



Change this



char userName;


to



char userName[10];


and then copy using strcpy() or strncpy(). For e.g



strcpy(userName, user[i].name);





share|improve this answer





















  • 2





    Never use strncpy (unless you have a very good reason that you can explain when asked about it). strncpy is a time bomb waiting to blow up in your face at any time.

    – gnasher729
    Dec 30 '18 at 15:21
















0














The problematic statement is



userName = user[i].name;
| |
char type char array type


firstly userName is declared as character variable and a character variable can't holds char array i.e user[i].name.



Change this



char userName;


to



char userName[10];


and then copy using strcpy() or strncpy(). For e.g



strcpy(userName, user[i].name);





share|improve this answer





















  • 2





    Never use strncpy (unless you have a very good reason that you can explain when asked about it). strncpy is a time bomb waiting to blow up in your face at any time.

    – gnasher729
    Dec 30 '18 at 15:21














0












0








0







The problematic statement is



userName = user[i].name;
| |
char type char array type


firstly userName is declared as character variable and a character variable can't holds char array i.e user[i].name.



Change this



char userName;


to



char userName[10];


and then copy using strcpy() or strncpy(). For e.g



strcpy(userName, user[i].name);





share|improve this answer















The problematic statement is



userName = user[i].name;
| |
char type char array type


firstly userName is declared as character variable and a character variable can't holds char array i.e user[i].name.



Change this



char userName;


to



char userName[10];


and then copy using strcpy() or strncpy(). For e.g



strcpy(userName, user[i].name);






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 30 '18 at 17:57

























answered Dec 30 '18 at 15:08









AchalAchal

9,1882830




9,1882830








  • 2





    Never use strncpy (unless you have a very good reason that you can explain when asked about it). strncpy is a time bomb waiting to blow up in your face at any time.

    – gnasher729
    Dec 30 '18 at 15:21














  • 2





    Never use strncpy (unless you have a very good reason that you can explain when asked about it). strncpy is a time bomb waiting to blow up in your face at any time.

    – gnasher729
    Dec 30 '18 at 15:21








2




2





Never use strncpy (unless you have a very good reason that you can explain when asked about it). strncpy is a time bomb waiting to blow up in your face at any time.

– gnasher729
Dec 30 '18 at 15:21





Never use strncpy (unless you have a very good reason that you can explain when asked about it). strncpy is a time bomb waiting to blow up in your face at any time.

– gnasher729
Dec 30 '18 at 15:21


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53978554%2fhow-to-assign-a-structure-member-to-a-variable-in-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas