delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/30/02:00:49

Message-ID: <34D1791E.F85DD72B@csun.edu>
Date: Thu, 29 Jan 1998 22:54:22 -0800
From: Ryan Bright <rmb56313 AT csun DOT edu>
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Allegro Datafiles - More Problems
References: <MPG DOT f39e7cbc8b4c11d9896bd AT news> <19980129094901 DOT EAA26113 AT ladder02 DOT news DOT aol DOT com> <MPG DOT f3a9094a793600b9896bf AT news>
NNTP-Posting-Host: s253n172.csun.edu
Organization: California State University, Northridge
Lines: 85
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Matt Riker wrote:
> 
> In article <19980129094901 DOT EAA26113 AT ladder02 DOT news DOT aol DOT com>,
> fist1000 AT aol DOT com says:
>  > >   draw_sprite(screen,datafile[spritename].dat,16,16);
>  > >spritename is a char with a 20 character array, containing "BLANK".
>  > First of all, you can't index an array with a string. :) When you save your
> Woops, I said that wrong.  Here is my variable declaration:
>    char spritename[20] = "BLANK";
> That's valid, right?
No!  I strongly suggest you get a book on C and study up on all your
terminology (and methodology, while you're at it).  Let's look at the
advice you received. :

"You can't index an array with a string."

Now your code :
char spritename[20] = "BLANK"; /* <-- A string */
draw_sprite(...datafile[spritename].dat...); /* Referencing an array
with a string */

Here's an initialized array.
int a[3] = {3, 2, 1};

Q.  What do you do if you want to reference member #0?
A.  a[0]

With an integer, right?

Here's another array.
datafile[] /* <- oversimplification */

Q.  What do you do if you want to reference member #n?
Your Answer   : datafile["BLANK"]
Correct Answer: datafile[n]

Make sense?

> The header and stuff's all taken care of with Grabber, and I've checked
> just to verify.  BLANK is declared in my datafile, as a BMP.
Okay.  You have been confusing #define's with strings (once again, find
a tutorial or a book [or write to comp.lang.c]).  To reference BLANK (as
defined in the header file outputted by grabber), use :
"datafile[BLANK].dat"  (This is extremely well-explained all throughout
the documentation (ie grabber.txt, html files, inf files, all over).

>  >   draw_sprite(screen, datafile[BLANK].dat,16,16);
> Except that I don't want the function to always draw BLANK to the
> screen.  I want to use my variable, spritename, because the sprite I
> want drawn is depending on which sprite the user chooses in my program.
I understand what you're trying to do, but you simply can't.  A #define
is what's called a preprocessor command.  So what happens is, before the
compiler does anything else, it scans through all your code looking for
the identifier BLANK (not "BLANK", the string) and replaces it with the
numerical value it has been assigned with (assigned by grabber), 

#define BLANK 2 /* for instance */

So, it is impossible to provide random access to your defines since
their names disappear long before the program is ever run.

/* BROKEN CODE */
printf("Which '#define' do you want to see?\n");
scanf("%s", &spritename);
printf("%s", spritename);

The above code, will absolutely not work (as it intends) under any
circumstances ever and that is basically what you're trying to do.

You basically have a couple options : 1) Ask for integer input (ie
"Which location in the datafile would you like to see?") and display
that directly--don't forget to make sure that it's the right type.  2)
Ask them to enter the name of the object AS IT APPEARS IN THE STRUCTURE
not in the header file. (It's under properties, read grabber.txt).

I would suspect that there are several other options, but they would all
tend to be variants of the above two.  However, if you are determined to
use the #define names, you can always say :

#include <string.h>
if (!strcmp(spritename, "BLANK")) blit(...datafile[BLANK].dat...)

...and continue for every define, but that is certainly messy.

-Ryan

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019