Thread: Assingment of an element of 2D character array to a 1D character array

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    2

    Assingment of an element of 2D character array to a 1D character array

    Can we do this :

    Code:
    char strings[][100]={"ABC","EFG","IJK","LKM"};
    char temp[100];
    temp=strings[1];
    Please help!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    No, although you can initialise an array, you cannot assign to an array.

    Next time, try and find out first, and then if the result does not conform to your expectations, come here and ask why.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2014
    Posts
    79
    While you can create "aliases" for any of the strings in your strings[] array, you can't copy their content with a simple assigment like temp=strings[1]. You have to copy the entire sequence of characters one by one with a loop, or you can take advantage from the services provided by the standard library strings.h. More precisely, have a look at the strcpy() function:

    Code:
    char *strcpy( char *destination, const char *source );
    Good luck!

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by Pingu View Post
    Can we do this :

    Code:
    char strings[][100]={"ABC","EFG","IJK","LKM"};
    char temp[100];
    temp=strings[1];
    Please help!
    You need to study the C Standard Library that is provided with all C compilers, One of the possible resources is:
    The C Library Reference Guide (Section 2.4, "String.h")

    Also, if programming on a Linux system, the "man" command is your best friend!
    "man 3 strcpy" (Section 3 is for programming.)

    You should also be aware that the C String Library is also one of the weakest parts of the programming in C. Please be aware that strcpy(), strcat(), gets(), and others can write beyond the end of the buffer. For example:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       char buff[5];
    
       strcpy(buff, "This is a test of a string larger than the buffer.");
       printf("%s\n", buff); /* Segmentation fault! (On my Linux system) */
    
       return 1; /* This program does NOT work correctly! */
    }
    You solution, with additional error checking could be:
    Code:
    char strings[][100]={"ABC","EFG","IJK","LKM"};
    char temp[100];
    /* error check for possible overflow before copying! */
    strcpy(temp, strings[1]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I remove a character from character array?
    By nick753 in forum C++ Programming
    Replies: 25
    Last Post: 12-08-2010, 11:27 AM
  2. Replies: 15
    Last Post: 09-23-2010, 02:19 PM
  3. REmoving a character from a character array
    By Bladactania in forum C Programming
    Replies: 3
    Last Post: 02-11-2009, 02:59 PM
  4. Replies: 8
    Last Post: 11-12-2008, 11:25 AM
  5. Replies: 7
    Last Post: 05-11-2008, 10:57 AM