TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Ryan Tomko
from: Kurt Kuzba
date: 1998-09-08 17:36:22
subject: C++ strings and arrays

RT>   char *name[9][9];
RT>   name[0]="Jonny";
RT>   and i get the message "Lvalue needed".. so..
RT>   can you explain what i need to do?
   Now you had a two dimensional pointer array. For the sake
   of illustration, we will represent each byte of a long
   pointer as a 0 in the following diagram:
char *name[arr][ptr];
/*
            arr0 arr1 arr2 arr3 arr4 arr5 arr6 arr7 arr8
      ptr0  0000 0000 0000 0000 0000 0000 0000 0000 0000
      ptr1  0000 0000 0000 0000 0000 0000 0000 0000 0000
      ptr2  0000 0000 0000 0000 0000 0000 0000 0000 0000
      ptr3  0000 0000 0000 0000 0000 0000 0000 0000 0000
      ptr4  0000 0000 0000 0000 0000 0000 0000 0000 0000
      ptr5  0000 0000 0000 0000 0000 0000 0000 0000 0000
      ptr6  0000 0000 0000 0000 0000 0000 0000 0000 0000
      ptr7  0000 0000 0000 0000 0000 0000 0000 0000 0000
      ptr8  0000 0000 0000 0000 0000 0000 0000 0000 0000
*/
   You can envision it as being like this in appearance.
   Each of the 0000's is a pointer value, which should,
   ideally, reference a string.
   Your code told the compiler to assign a string pointer
   to the first pointer array, or arr0. It can't do that.
   The string pointers are referenced by the secondary
   array index. If you had used
name[0][0] = "Johnny";
   or maybe even
*name[0] = "Johnny"
   then it would have worked.
   But perhaps a 9x9 array of pointers is overkill anyway.
   You could just use a single pointer array. If you are
   uncertain as to the data being entered, then you may
   wish to manually truncate the data before copying so as
   to avoid overrunning your allocated space.
   Have a look at this example code. It should run for you.
#include        /*   for fgets(), printf(), sprintf()   */
#include                 /*   for strcpy(), strchr()   */
#include          /*   for standard malloc(), free()   */
void GetStr(char *S, int L)
{
   char buf[10] = "";
   fgets(S, L, stdin);
   if(strchr(S, '\n'))
      *(strchr(S, '\n')) = (char)'\0';
   else
      while(!strchr(buf, '\n'))
         fgets(buf, 10, stdin);
}
int main(void)
{
   char *name[9] = { NULL };  /*   initializing makes all NULL   */
   char buf[16] = "";
   char job[3][14];    /*   This is a string array on the stack  */
   name[0] = "Johnny";   /*   name[0] points to a string literal */
   name[1] = malloc(16); /*   allocate space to pointer name[1]  */
   printf("Enter your name [ 15 characters MAX. ]=> ");
   GetStr(name[1], 16);
   name[2] = buf;
   sprintf(buf, "%.15s", "Jimmy");     /*  specifier
sets limit  */
   printf(" Name[0] = %s \n", name[0]);
   printf(" Name[1] = %s \n", name[1]);
   printf(" Name[2] = %s \n", name[2]);
   free(name[1]);   /*  free malloc()'ed space when not needed   */
   sprintf(job[1], "%.13s", "Mailman");
   strcpy(job[0], "Endocrinologist!");   /*   This will mess up   */
   printf(" job[0] = %s \n", job[0]);
   printf(" job[1] = %s \n", job[1]);
   /*   Notice how writing to job[0] overran into job[1].
         That is why I chose to use sprintf() for job[1] and
         for name[2]. You have to carefully observe your array
         boundaries. C will not mind them for you.               */
   strcpy(job[0], "Endocrinologist!");
   sprintf(job[1], "%.13s", "Mailman");
   printf(" job[0] = %s \n", job[0]);
   printf(" job[1] = %s \n", job[1]);
   /*   Notice how the job array has again been messed up by
        writing a string too large into the memory allocated.
        If this were to happen in an uncontrolled situation,
        valuable data or code might be overwritten. In one
        instance, I overwrote a section of a jump table and
        ended up with my code jumping to the CMOS BIOS
        configuration writing routines, which erased my BIOS
        data. I had to reconfigure my system before it would
        boot. The majority of the program-stopping bugs
        created by novice programmers involve out-of-bounds
        errors with arrays.
   */
   return 0;
}

> ] Their numbers are 664 and 668, the neighbors of the beast...

---
* Origin: *YOPS ]I[* 8.4 GIG * RA/FD/FE * Milwaukee, WI (1:154/750)
SEEN-BY: 396/1 622/419 632/0 371 633/260 267 270 371 634/397 635/506 728 810
SEEN-BY: 639/252 670/213 218
@PATH: 154/750 222 396/1 633/260 635/506 728 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.