Thread: unable to set char array

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    3

    unable to set char array

    Code:
    #include <stdio.h>
    char * strcpy(char *restrict s1, const char *restrict s2);
    struct item
    {
        char title[20];
        struct item *next;
    } firstCard;
    
    int main () 
    {
        strcpy(firstCard.next->title, "whatever");
    }
    Please help me understand what I am doing wrong here. I am unable to set firstCard.next->title

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char * strcpy(char *restrict s1, const char *restrict s2);
    You should #include <string.h> rather than inventing your own prototype.

    > strcpy(firstCard.next->title, "whatever");
    You need to make firstCard.next point to some valid memory, before you apply the -> operator to it.

    Say
    firstCard.next = malloc( sizeof(*firstCard.next));

    then you can do the copy.
    Don't forget to #include <stdlib.h> as well.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    3
    thank you very much, Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char array doesn't print the char in the array
    By KiRa11Love in forum C Programming
    Replies: 4
    Last Post: 09-07-2012, 05:52 AM
  2. Replies: 4
    Last Post: 05-17-2009, 03:28 AM
  3. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  4. signed char array to unsign char array.
    By beon in forum C Programming
    Replies: 5
    Last Post: 12-14-2006, 07:19 PM
  5. Unable to allocate array size at runtime
    By CookieMonster in forum C Programming
    Replies: 16
    Last Post: 04-03-2004, 06:48 PM

Tags for this Thread