Thread: Little question about structures...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    12

    Little question about structures...

    Hello,

    sorry if this seems like a trivial query but I was having some issue setting a string to a char array in a structure. I have a set up like this:


    Code:
    Code:
    struct Computer  
    {  
    char Manufacturer[30];  
    float Price;  
    int Memory;  
    } *PC;

    i need to set the Manufacturer to HAL,using only the arrow operator. how can i do that?

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Last edited by Epy; 12-17-2009 at 07:32 AM. Reason: added link for reference

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't assign arrays with the assignment operator.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    strncpy(PC->Manufacturer, "HAL", 4);
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by jeffcobb View Post
    strncpy(PC->Manufacturer, "HAL", 4);
    There's no point in passing the size of the source buffer to strncpy. To avoid buffer overruns, it should actually look more like:

    Code:
    strncpy(PC->Manufacturer, "HAL", sizeof(PC->Manufacturer));
    PC->Manufacturer[sizeof(PC->Manufacturer)-1] = '\0'; // since strncpy() isn't guaranteed to null terminate
    Of course if you know that the string will fit, you can just use strcpy() instead.
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  2. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  3. Replies: 4
    Last Post: 02-02-2003, 05:45 AM
  4. Question about structures
    By Randoon in forum C Programming
    Replies: 2
    Last Post: 12-12-2002, 11:47 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM

Tags for this Thread