help with initializing a char array

This is a discussion on help with initializing a char array within the C Programming forums, part of the General Programming Boards category; is it possible to initialise a char array separately from where it is declared without using either a for loop ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    7

    help with initializing a char array

    is it possible to initialise a char array separately from where it is declared without using either a for loop or initialising the array character by character?

  2. #2
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Yes.
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You can use string functions. but you cannot assign since array is not first-class in C.
    Eg.
    Code:
    char str[100];
    strcpy(str,"hello");

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    If you can use c99, I believe using compound literals is the closest to what your asking.

    Code:
    char *arr;
    arr = ((char []){"Hello"});

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Well, you are assigning pointer. not array.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Just like you I provided an alternative solution, I don't see the problem? I was clear that it's the closest thing (I know of) to an assignment. After the assignment arr, behaves and functions just like a normal array in terms of indexing and so on.
    Last edited by Subsonics; 03-21-2011 at 07:02 AM.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Because OP is asking 'is it possible to initialise a char array separately from where it is declared'.
    You canNOT assign to an array. C (including C99) only allows to initialize array.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Yes, so the correct answer to the question is, NO. What both you and me are doing here is to offer an alternative solution.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can cheat.
    Code:
    #include<stdio.h>
    struct foo
    {
        char bar[10];
    };
    #define CHEAT(x,y) *((struct foo *)(x)) = (y)
    #define CHEATA(x,y) *((struct foo *)(x)) = *((struct foo *)(y))
    int main( void )
    {
        char bar[10] = {0};
        char foo[10] = { "world" };
        struct foo baz = { "hello" };
        
        CHEAT(bar, baz) ;
        printf( "I cheated: %s\n", bar );
        
        CHEATA( bar, foo ) ;
        printf( "I cheated: %s\n", bar );
        
        return 0;
    }
    Technically it's not assigning arrays, so no, you can't actually assign an array. But you can cheat your way around it.


    Quzah.
    Last edited by quzah; 03-21-2011 at 03:11 PM. Reason: added CHEATA
    Hope is the first step on the road to disappointment.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    Quote Originally Posted by csepraveenkumar View Post
    is it possible to initialise a char array separately from where it is declared without using either a for loop or initialising the array character by character?
    Sure it is. People typically use memset for this in C.
    I guess it kinda depends on what you want to initialise it with though.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by iMalc View Post
    Sure it is. People typically use memset for this
    ...which uses a loop. You aren't using a loop directly, but the function you are calling does.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help :(
    By ramenen in forum C++ Programming
    Replies: 1
    Last Post: 02-17-2010, 03:31 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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