xboxscene.org forums

OG Xbox Forums => Software Forums => Development => Topic started by: rtheil on January 18, 2005, 03:58:00 PM

Title: Quick Array Question
Post by: rtheil 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.
Title: Quick Array Question
Post by: JapanFred on January 18, 2005, 04:22:00 PM
QUOTE(rtheil @ Jan 19 2005, 12:29 AM)

Title: Quick Array Question
Post by: rtheil 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?

Title: Quick Array Question
Post by: JapanFred 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:
Title: Quick Array Question
Post by: rtheil on January 18, 2005, 05:32:00 PM
OH! Now I get it. Thanks a bunch...
Title: Quick Array Question
Post by: JapanFred on January 18, 2005, 05:35:00 PM
No problem mate, is it all compiling and running correctly now?
Title: Quick Array Question
Post by: rtheil 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?
Title: Quick Array Question
Post by: dankydoo on January 18, 2005, 08:49:00 PM
QUOTE(JapanFred @ Jan 19 2005, 12:57 AM)
CODE

Title: Quick Array Question
Post by: rtheil 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.
Title: Quick Array Question
Post by: dankydoo 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
Title: Quick Array Question
Post by: rtheil on January 19, 2005, 08:09:00 AM
That's it. %S. Damn, I almost nearly ran through the entire alphabet!

Thanks!!
Title: Quick Array Question
Post by: Butcher_ on January 19, 2005, 05:13:00 PM
QUOTE(JapanFred @ Jan 19 2005, 01:57 AM)
CODE

Title: Quick Array Question
Post by: d0wnlab 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");
Title: Quick Array Question
Post by: JapanFred on January 19, 2005, 09:29:00 PM
Well i guess we all learny something here.
Title: Quick Array Question
Post by: rtheil 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.
Title: Quick Array Question
Post by: JapanFred on January 20, 2005, 10:38:00 AM
Does is return the file AND them characters? Or just them characters?
Title: Quick Array Question
Post by: rtheil on January 20, 2005, 10:58:00 AM
within the function it returns the characters out of the file. the return from the function is displaying only those characters. The first line conf[0] has the weird characters, and the second line conf[1] seems to be blank.
Title: Quick Array Question
Post by: d0wnlab on January 20, 2005, 11:32:00 AM
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 "";
   }
}


Okay.  Basically, there are two different parts of memory from which you can allocate space for variables. when you do char line[100], it creates the space on the stack.  This is good for temporary variables because the compiler takes care of allocation and deallocation.  To be exact, that memory is considered defined at the start of that function, and undefined at the end of that function.  Since you want to pass chunks of memory around between functions you need to allocate space on the heap for the strings you want to copy around.  However, there's nothing wrong with first copying the line to a temp stack buffer.  Also, in C, when you use [] you are basically dereferencing into an array right? (like, give me the nth item in the array).  Well as we learnt above in this thread, a string is nothing more than an array of characters.  My point is that you don't actually pass back a char *, but a char ** because you are sending back a pointer to a list of pointers of character arrays.  You should read the C programming thing on howstuffworks.com about pointers and it should make it clearer.  

That pointer thing is also why you were returning *conf when I'd bet you originally had conf and the compiler complained smile.gif

anyways, You should do something in there more like this:

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);
        // allocate space on the heap for the string
        conf = (char *) malloc(sizeof(char) * (strlen(line) + 1));
        // copy the string from the temp buffer to our returned value
     strcpy(conf, line);
     i++;
  }
  fclose(fp);
  return conf;
   }
   else
   {
  return 0x00;  // you should use nulls when dealing with pointers in general to deal with errors.
   }
}



This was written on the fly and I just woke up but it should work I think.
Title: Quick Array Question
Post by: Butcher_ on January 23, 2005, 09:51:00 AM
CODE

char** getConfig()
{
  debug("Opening Config file");
  FILE *fp;
  if((fp = fopen("D:\\xblock.xml","r")) != NULL)
  {
    char **conf;
    char line[100];

    conf = malloc(2 * sizeof(char*));
    memset(conf, 0, 2 * sizeof(char*));
    
    //loop through lines
    for (int i = 0; !feof(fp) && i < 2; ++i)
    {
      fgets(line, 50, fp);
      debug(line);
      // allocate space on the heap for the string and copy it in
      conf = strdup(line);
    }
    fclose(fp);
    return conf;
  }
  else
  {
    return NULL;  // you should use nulls when dealing with pointers in general to deal with errors.
  }
}


Don't forget to free each element of conf and conf itself afterwards:
CODE

for (int i = 0; i < 2; ++i)
  free(conf);
free(conf);