Thread: copying strings does not work, why

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87

    copying strings does not work, why

    Hello.

    I am trying to copy the content of one string into an another. But it does not work. Why not? Here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    int main(){
    
        char *b[200][3];
        char *left1[200]={""};
    
    
        b[5][1] = "John";
    
        strcpy(left1[5], b[5][1]);
    
        return 0;
    }
    Last edited by nerio; 09-08-2017 at 02:16 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,067
    Left1 doesn't have any allocated space you can write to.

    Try
    Left1 [200][10]
    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
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    Salem, a little off topic but I got a question:
    Why is the code posted on here often so cryptic? Everyone uses one letter variable names, nondescriptive names, etc...

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    728
    Quote Originally Posted by Asymptotic View Post
    Salem, a little off topic but I got a question:
    Why is the code posted on here often so cryptic? Everyone uses one letter variable names, nondescriptive names, etc...
    There are way too many bad instructors teaching C that do not advise their students to use descriptive names for functions and variables, with the exception of loop variables.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying strings, all or nothing?
    By Alpo in forum C Programming
    Replies: 13
    Last Post: 04-15-2014, 02:22 AM
  2. Help with copying strings
    By eddieL91 in forum C Programming
    Replies: 5
    Last Post: 04-10-2012, 11:53 AM
  3. K&r 1-17 copying strings
    By AaronP in forum C Programming
    Replies: 4
    Last Post: 11-18-2011, 11:28 PM
  4. Need help copying files into strings???
    By patso in forum C Programming
    Replies: 5
    Last Post: 08-18-2010, 11:22 PM
  5. Problems with copying strings
    By Veneficvs in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2010, 07:40 AM

Tags for this Thread