Thread: array inside structure

  1. #1
    Registered User blob84's Avatar
    Join Date
    Jun 2010
    Posts
    46

    array inside structure

    Hi, i want to assign an array inside a structure this is the code:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    int main() {
    	struct print {
    		char *head[6];
    	};
    	
    	struct print omino;
    	
    	omino.head[0] = "     __________________\n";
    	omino.head[1] = "    |                  \\    _______\n";
    	omino.head[2] =	"    |                   \\  |       |\n";
    	omino.head[3] =	"    |                    \\ |       |\n";
    	omino.head[4] =	"    |                     \\|       |\n";
    	omino.head[5] =	"    |                      |_______|\n";
    	
    	int i;
    	for( i = 0; i < 6; i++ ) printf("%s", omino.head[i]);			   
    	
    }
    this works, but if i want to assign the array like:
    Code:
    omino.head[] = { "string" ... }
    i get syntax error.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    35
    You can't assign to an array like that, except during initialization.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can initialize the structure when you're creating it. And since it contains only the array (which really should be const char*), you can simply initialize those members without worrying about the rest.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Initializing an array inside of a structure
    By Lima in forum C Programming
    Replies: 6
    Last Post: 06-08-2009, 08:53 PM
  3. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  4. How to sort an array of pointers to structure
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 06-30-2008, 02:52 PM
  5. Realloc problems with sturcture array inside structure
    By daveyand in forum C Programming
    Replies: 2
    Last Post: 03-29-2004, 06:48 AM