Thread: Arrays

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    2

    Arrays

    Just a quick question about multidemintional arrays, i understand that when i create them i can put values into them with = {values} however is there anyway to put the values in them the sameway after ive created them (without a loop of somekind)?

    Heres the code ive got, FONTHEIGHT and FONTWIDTH are #defines of 8 and 4. chr is just an int.
    Code:
             int FontArray[FONTHEIGHT][FONTWIDTH];
    	switch (chr)
    	{
    		// H
    	case 72: FontArray[][] = { {1, 0, 0, 1 }, 
    					      {1, 0, 0, 1 }, 
    					      {1, 0, 0, 1 }, 
    					      {1, 1, 1, 1 },
    					      {1, 1, 1, 1 },
    					      {1, 0, 0, 1 },
    					      {1, 0, 0, 1 },
    					      {1, 0, 0, 1 }}; break;
    	default: FontArray[][] = { {1, 1, 1, 1 }, 
    					     {1, 1, 1, 1 }, 
    					     {1, 0, 0, 1 }, 
    					     {1, 0, 0, 1 },
    					     {1, 0, 0, 1 },
    					     {1, 0, 0, 1 },
    					     {1, 0, 0, 1 },
    					     {1, 1, 1, 1 }}; break;
    	}

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Re: Arrays

    Originally posted by H3g3m0n
    however is there anyway to put the values in them the sameway after ive created them (without a loop of somekind)?
    No.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You could always use pointers...
    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    
    #define FONTHEIGHT 8
    #define FONTWIDTH  4
    
    int main ( int argc, char *argv[] ) {
      int FontArrays[2][FONTHEIGHT][FONTWIDTH] = {
        {
          {1, 0, 0, 1 }, 
          {1, 0, 0, 1 }, 
          {1, 0, 0, 1 }, 
          {1, 1, 1, 1 },
          {1, 1, 1, 1 },
          {1, 0, 0, 1 },
          {1, 0, 0, 1 },
          {1, 0, 0, 1 }
        },
        {
          {1, 1, 1, 1 }, 
          {1, 1, 1, 1 }, 
          {1, 0, 0, 1 }, 
          {1, 0, 0, 1 },
          {1, 0, 0, 1 },
          {1, 0, 0, 1 },
          {1, 0, 0, 1 },
          {1, 1, 1, 1 }
        },
      };
      int r, c;
      int chr = 72;
      int (*fontptr)[FONTWIDTH];
      switch (chr)
      {
          case 72: fontptr = FontArrays[0]; break;
          default: fontptr = FontArrays[1]; break;
      }
      for ( r = 0 ; r < FONTHEIGHT ; r++ ) {
        for ( c = 0 ; c < FONTWIDTH ; c++ ) {
          printf( "%d", fontptr[r][c] );
        }
        printf( "\n" );
      }
      return 0;
    }
    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.

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    2
    Thanks, I guess ill probally have to find some otherway of retreving them, pointers probally wouldn't work as i would need atleast although many numbers would be returning the same array.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > although many numbers would be returning the same array.
    Like

    case 72: case 73: case 74: case 75: fontptr = FontArrays[0]; break;

    In fact, why not just go for it and say
    fontptr = FontArrays[chr];
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM