Thread: struct

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    struct

    I started learning structures and I've some problems(see below)which I don't know why when I compile the code, it either doesn't work at all and clearing out an error, or the code work but there's a misunderstanding in the code's script.
    P.S: I was having another thread related to structures but I can't ask in the same thread more than one issue!

    problem 1:
    when I want to print a simple code for printing a char by using a struct, but the result is something like "?" not a char at all!
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    struct Test
    {
        char x;
    };
     
    int main(void)
    {
        struct Test serTest;
     
        serTest.x ="f"; // the debugger shows there's an error here, idk why!! //
     
        printf("x=%c\n",serTest.x); // it should print here the char value that's stored into serTest..//
    
    
        return 0;
    }
    Problem 2:
    while I was collecting information about structures there was accidently an example which is shown below, the case isn't in the code itself (not in the script itself)....the issue is, in implementing the notes that I wrote them in "//" (in red color)near every code I would get a holy ........ bugs!; I've colored the notes in Red color for telling me please why I can't implement what I've written in the notes.
    Code:
    #include<stdio.h>
    struct Test
    {
        int x;
    };
     
    void updateStruct(struct Test *y);   // here, why I can't for example write instead of the current function, a function "updateStruct(struct Test y)" without using a pointer itself? I tried to remove "*" but it says there's error, so why can't?//
     
    int main(void)
    {
        struct Test sTest;
     
        sTest.x = 2;
     
        printf("x = %d\n",sTest.x);
     
        updateStruct(&sTest); // and here why I cant write instead of &sTest, an equivalent one as a sTest.x? or actually as a sTest (without &)?//
    //what does even the operator "&" near sTest in updateStruct function stand for? can't get ride of it? // 
     
        printf("x = %d\n",sTest.x);
     
        return 0;
    }
     
    void updateStruct(struct Test *y) // here why I actually need a pointer in struct at all? I can use the general way just write " y " without "*" (meaning without the pointer)//
    {
        (*y).x = 3;  // line of interest
    }
    If you explain properly what I've asked in upper including the notes, I would appreciate your assistance and I will be wholly known of the structures absolutely!



    thanks alot!!
    Last edited by RyanC; 05-23-2015 at 01:58 PM.

  2. #2
    Registered User
    Join Date
    May 2014
    Posts
    121
    "f" is a string. Use 'f' if you want a single char.

  3. #3
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by MOS-6581 View Post
    "f" is a string. Use 'f' if you want a single char.
    the same result, doesn't work as its!

  4. #4
    Registered User
    Join Date
    May 2013
    Posts
    228
    Quote Originally Posted by RyanC View Post
    I started learning structures and I've some problems(see below)which I don't know why when I compile the code, it either doesn't work at all and clearing out an error, or the code work but there's a misunderstanding in the code's script.
    P.S: I was having another thread related to structures but I can't ask in the same thread more than one issue!

    problem 1:
    when I want to print a simple code for printing a char by using a struct, but the result is something like "?" not a char at all!
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    struct Test
    {
        char x;
    };
     
    int main(void)
    {
        struct Test serTest;
     
        serTest.x ="f"; // the debugger shows there's an error here, idk why!! //
     
        printf("x=%c\n",serTest.x); // it should print here the char value that's stored into serTest..//
    
    
        return 0;
    }
    Like MOS-6581 suggested, try changing:

    Code:
    serTest.x ="f";
    to:

    Code:
    serTest.x ='f';


    Quote Originally Posted by RyanC View Post
    // here, why I can't for example write instead of the current function, a function "updateStruct(struct Test y)" without using a pointer itself? I tried to remove "*" but it says there's error, so why can't?
    You can pass structs by value, as far as I know, that means the struct's values will by copied to the stack frame of the called function. This is not always a desirable behaviour, though, and depends on your intentions for the struct in question, and sometimes your memory limitations.

    Quote Originally Posted by RyanC View Post
    // and here why I cant write instead of &sTest, an equivalent one as a sTest.x? or actually as a sTest (without &)?//
    //what does even the operator "&" near sTest in updateStruct function stand for? can't get ride of it? //
    The & sign, in this context, is the address operator. It's quite an important one, actually, and you should find it in every basic C tutorial or C programming book.

    Quote Originally Posted by RyanC View Post
    // here why I actually need a pointer in struct at all? I can use the general way just write " y " without "*" (meaning without the pointer)//
    Again, you can, but it really comes down to what you are trying to do.
    In your case, the name of the function implies that you shouldn't.
    Last edited by Absurd; 05-23-2015 at 04:32 PM.

  5. #5
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by Absurd View Post
    Like MOS-6581 suggested, try changing:
    The & sign, in this context, is the address operator. It's quite an important one, actually, and you should find it in every basic C tutorial or C programming book.
    I understand you, to be more assure you mean like when I put "&sTest" I call for the address function that sTest is the name of...then we go to the function to get the values from it or to do whatever things the called address function included, ye? e.g: because I wrote "struct Test sTest" then the book's adrress is Test, and it's named as sTest, then when I write "&sTest", I would go to the address of the sTest which is in this case to "struct Test"..and pass over its codes.., am I right?

  6. #6
    Registered User
    Join Date
    May 2013
    Posts
    228
    Quote Originally Posted by RyanC View Post
    I understand you, to be more assure you mean like when I put "&sTest" I call for the address function that sTest is the name of...then we go to the function to get the values from it or to do whatever things the called address function included, ye? e.g: because I wrote "struct Test sTest" then the book's adrress is Test, and it's named as sTest, then when I write "&sTest", I would go to the address of the sTest which is in this case to "struct Test"..and pass over its codes.., am I right?
    If I understand you correctly, then no. You are not right.
    I recommend that you sit and learn this stuff from the basics: find some online C tutorial that you feel comfortable with, or even better; loan a C programming book from your close-to-home university's library, and start learning this from scratch, because judging from this post and others you posted here; you lack basic understanding of the C programming language.
    I'm not trying to patronise, this is sincere. My shelf has a book to almost every popular programming language, and sure, I admit, I didn't read them all front to back, but when I write code, they are laid out open near by.
    Last edited by Absurd; 05-24-2015 at 03:32 AM.

  7. #7
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by Absurd View Post
    If I understand you correctly, then no. You are not right.
    I recommend that you sit and learn this stuff from the basics: find some online C tutorial that you feel comfortable with, or even better; loan a C programming book from your close-to-home university's library, and start learning this from scratch, because judging from this post and others you posted here; you lack basic understanding of the C programming language.
    I'm not trying to patronise, this is sincere. My shelf has a book to almost every popular programming language, and sure, I admit, I didn't read them all front to back, but when I write code, they are laid out open near by.
    I just found it hard for this subject specifically, anyone I will read more about it and you can close this thread.
    thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting array of struct inside struct
    By blueboyz in forum C Programming
    Replies: 13
    Last Post: 04-24-2012, 02:15 AM
  2. Replies: 5
    Last Post: 06-30-2011, 03:24 PM
  3. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  4. 2 problems: Struct array and pointer + struct
    By v1n1c1u5 in forum C Programming
    Replies: 0
    Last Post: 12-13-2009, 05:38 PM
  5. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM