xboxscene.org forums

Pages: [1] 2

Author Topic: Quick Array Question  (Read 245 times)

rtheil

  • Archived User
  • Full Member
  • *
  • Posts: 111
Quick Array Question
« on: January 18, 2005, 03:58:00 PM »

OK, so I've been reading all over the place about how to set up arrays in C++, but I completely do not understand string arrays. What I'm trying to do is set up an array of like 5-10 different messages in several different languages, then loop through it, then display em all. I just don't get it though. I'm a hardcore php programmer, but this is definitely a different world.
Logged

JapanFred

  • Archived User
  • Full Member
  • *
  • Posts: 116
Quick Array Question
« Reply #1 on: January 18, 2005, 04:22:00 PM »

QUOTE(rtheil @ Jan 19 2005, 12:29 AM)

Logged

rtheil

  • Archived User
  • Full Member
  • *
  • Posts: 111
Quick Array Question
« Reply #2 on: January 18, 2005, 04:59:00 PM »

OH! That makes so much more sense. However, when compiling I get a warning:

Warning C4700 local variable 'msgArray' used without having been initialized.

This warning is on both lines with strcpy.

CODE

char *msgArray[10];
strcpy(msgArray[0], "Message1");
strcpy(msgArray[1],"Message2");


I thought I initialized it on the first line, or am I missing something?

Logged

JapanFred

  • Archived User
  • Full Member
  • *
  • Posts: 116
Quick Array Question
« Reply #3 on: January 18, 2005, 05:26:00 PM »

QUOTE(rtheil @ Jan 19 2005, 01:30 AM)
OH! That makes so much more sense. However, when compiling I get a warning:
Logged

rtheil

  • Archived User
  • Full Member
  • *
  • Posts: 111
Quick Array Question
« Reply #4 on: January 18, 2005, 05:32:00 PM »

OH! Now I get it. Thanks a bunch...
Logged

JapanFred

  • Archived User
  • Full Member
  • *
  • Posts: 116
Quick Array Question
« Reply #5 on: January 18, 2005, 05:35:00 PM »

No problem mate, is it all compiling and running correctly now?
Logged

rtheil

  • Archived User
  • Full Member
  • *
  • Posts: 111
Quick Array Question
« Reply #6 on: January 18, 2005, 07:30:00 PM »

OK, so now, when I try to use the variable msgArray[0] or even msgArray, I get nothing but numbers. There's something I need to do to it first, right?
Logged

dankydoo

  • Archived User
  • Full Member
  • *
  • Posts: 145
Quick Array Question
« Reply #7 on: January 18, 2005, 08:49:00 PM »

QUOTE(JapanFred @ Jan 19 2005, 12:57 AM)
CODE

Logged

rtheil

  • Archived User
  • Full Member
  • *
  • Posts: 111
Quick Array Question
« Reply #8 on: January 18, 2005, 09:02:00 PM »

Wow, that explains a lot. It actually answers a couple of questions I had.

OK, let me clarify my problem above.

after converting the item from my array to the sText variable, I am using this to output to a buffer to print to the screen.

swprintf(szbuff, L"%s", sText);

%s outputs nothing, and %d outputs numbers. My debugging doesn't work, so this is kinda hard. I created a debug function that writes to a file, and I know that the sText variable does in fact contain the string I want to print out.

Thanks again.
Logged

dankydoo

  • Archived User
  • Full Member
  • *
  • Posts: 145
Quick Array Question
« Reply #9 on: January 18, 2005, 10:05:00 PM »

it is because swprintf is for printing into a widchracter buffer and you are printing a regular string into it.  I'm not sure what the correct %s it might be a captial %S
Logged

rtheil

  • Archived User
  • Full Member
  • *
  • Posts: 111
Quick Array Question
« Reply #10 on: January 19, 2005, 08:09:00 AM »

That's it. %S. Damn, I almost nearly ran through the entire alphabet!

Thanks!!
Logged

Butcher_

  • Archived User
  • Full Member
  • *
  • Posts: 181
Quick Array Question
« Reply #11 on: January 19, 2005, 05:13:00 PM »

QUOTE(JapanFred @ Jan 19 2005, 01:57 AM)
CODE

Logged

d0wnlab

  • Archived User
  • Sr. Member
  • *
  • Posts: 326
Quick Array Question
« Reply #12 on: January 19, 2005, 05:48:00 PM »

yeah.

If you want to write to the strings after defining them, do something like this:

char *msgArray[10];

msgArray[0] = (char *) malloc(sizeof(char) * (desired_len + 1));

strcpy(msgArray[0], "string of length desired_len but no more");
Logged

JapanFred

  • Archived User
  • Full Member
  • *
  • Posts: 116
Quick Array Question
« Reply #13 on: January 19, 2005, 09:29:00 PM »

Well i guess we all learny something here.
Logged

rtheil

  • Archived User
  • Full Member
  • *
  • Posts: 111
Quick Array Question
« Reply #14 on: January 20, 2005, 08:03:00 AM »

Wow... This is awesome! I'm learning tons too. OK, I have one more question regarding all this.

So, within a function I read from a local config file (2 lines), and return that array. So, within the function, all values are there, but they come out all funky after returned. Here's my code:

CODE

char* getConfig()
{
   debug("Opening Config file");
   FILE *fp;
   if((fp = fopen("D:\\xblock.xml","r")) != NULL)
   {
  char *conf[2] = {NULL};
  char line[100];
  int i = 0;
  //loop through lines
  while (!feof(fp)){
     fgets(line, 50, fp);
     debug(line);
     conf = line;
     i++;
  }
  fclose(fp);
  return *conf;
   }
   else
   {
  return "";
   }
}


Then when calling this function:

CODE

char *conf[2] = {NULL};
*conf = getConfig();


But it returns stupid stuff like:

ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ


Please feel free to tell me how horribly written any of that is. Thanks again for all the teaching!

NOTE: debug() function is just a simple function that writes stuff to a file since I can't get my debugging to work.
Logged
Pages: [1] 2