Like Tree1Likes
  • 1 Post By std10093

struct and pointers

This is a discussion on struct and pointers within the C Programming forums, part of the General Programming Boards category; I have a variable (Mydict) which is struct type, which includes a pointer (dict0) to an array of chars (5 ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    10

    struct and pointers

    I have a variable (Mydict) which is struct type, which includes a pointer (dict0) to an array of chars (5 of them). now I want to assign a word to that array and can't figure out how to do it.
    I can't change the lines in the struct definition itself so I need to know how to write the last line correctly (the one that's supposed to assign "olga" to the array.


    Code:
    int main(){
        struct dict{
            int length;
            char (*dict0)[5];
        };
        struct dict Mydict;
        Mydict.dict0="Olga";
    
    
      return 0;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    strcpy is the answer.

    In C we have to use the string.h library for string handling.

    Also the struct does not seem ok.
    First of all, when we want to access a pointer into a struct we use the operator -> , instead of the dot.
    Secondly what you need, is just an array of characters.
    Code:
    char dict[5];
    Try naming your variables in a more meaningful way
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > char (*dict0)[5];
    This is a pointer (just one) to an array of 5 chars.
    Contrast with char *dict0[5] which is an array of 5 pointers to char

    You would need
    Code:
    Mydict.dict0 = malloc( sizeof(*Mydict.dict0) );
    strcpy(Mydict.dict0,"test");  // copy at most 4 chars
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    10
    I don't quite understand why I need malloc. don't I already assign the memory in the struct definition when I define the dict0 points to 5 characters long array?

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    10
    thanx Salem. I think you just solved my problem. although I had to add '*' before Mydict in the strcpy function.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,101
    Quote Originally Posted by littlerunaway View Post
    I don't quite understand why I need malloc. don't I already assign the memory in the struct definition when I define the dict0 points to 5 characters long array?
    If you don't want to use malloc you need a buffer somewhere that you can point to.

    Code:
    static char buffer[5] = {0};
    
    int main(void){
        struct dict{
            int length;
            char (*dict0)[5];
        };
        struct dict Mydict;
        
        {   
            char (*ptr)[5] = &buffer;
            Mydict.dict0 = ptr;
        }
        strcpy(*Mydict.dict0, "test");
        printf("*Mydict.dict0: %s\n", *Mydict.dict0);
     
        return 0;
    }
    If you don't want to use the temporary variable you can also use the typecast

    Mydict.dict0 = (char(*)[5])&buffer;

    But this looks quite ugly to me. What's wrong with just making your member a char** ?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,754
    Quote Originally Posted by c99tutorial
    If you don't want to use the temporary variable you can also use the typecast

    Mydict.dict0 = (char(*)[5])&buffer;

    But this looks quite ugly to me.
    I don't see why you cannot just write:
    Code:
    Mydict.dict0 = &buffer;
    since buffer is a char[5], hence &buffer is already a char(*)[5].
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,101
    That's correct. However in the general case you probably would declare a larger buffer and want to allocate from it

    Code:
    static char buffer[10000] = {0};
    Then you would need the typecast, for example, to allocate 5 bytes from offset 100:

    Code:
    Mydict.dict0 = (char(*)[5])&(buffer[100]);
    That's why I would say to make it char ** to simplify the notation. Or consider typedef if you really need the [5] for compiler type safety.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    1
    Thank you all! It is usefull for me to fix my programming errors.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    two accounts? Not good.
    laserlight likes this.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  2. Want help using struct pointers
    By burkoJames in forum C Programming
    Replies: 2
    Last Post: 05-13-2010, 10:59 PM
  3. Struct as pointers
    By wirefree101 in forum C Programming
    Replies: 8
    Last Post: 12-03-2009, 01:48 PM
  4. pointers to struct - help please
    By seminolas in forum C Programming
    Replies: 2
    Last Post: 12-22-2008, 12:03 AM
  5. Assigning pointers from pointers in struct.
    By Brian in forum C Programming
    Replies: 2
    Last Post: 10-18-2003, 04:30 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21