Constant string array addressing

This is a discussion on Constant string array addressing within the C++ Programming forums, part of the General Programming Boards category; Hello, My C++ code is rusty. I haven't used it for quite some time. As a result, certain structures and ...

  1. #1
    Lar
    Lar is offline
    Registered User
    Join Date
    Nov 2011
    Posts
    1

    Question Constant string array addressing

    Hello,

    My C++ code is rusty. I haven't used it for quite some time. As a result, certain structures and how to access them have been at least temporarily forgotten. I have the following array, that I want to search in order to find the corresponding Table (SQL) name. It's not quite working out the way I wish. It doesn't seem to be indexing by element, but rather by character.

    filedata defined as:
    Code:
    const char filedata [][20] = {
       "fileId\0",
       "fileid\0",
       "filename\0",
       "filename\0",
       "filesize\0",
       "filesize\0",
       "fileurl\0",
       "fileURL\0",
       "filetitle\0",
       "title\0",
       "filecontent\0",
       "content\0",
       "Description\0",
       "description\0",
       "fileimage\0",
       "imageURL\0",
       "fileimagestyle\0",
       "imagestyle\0",
       0
    };
    Keytable is declared in class Sysinfo:

    Code:
    class Sysinfo
    {
    private:
    public:
       ifstream InFile;
    
            ...
    
       char     *Key;
       long     KeySz;
       char     *Value;
       long     ValueSz;
       char     *KeyTable;
             ...
    };
    The following code does not work as desired
    Code:
          KeyTable = (char *)&filedata;
          Index = 0;
          while(KeyTable[Index])
          {
             if(!(strcmp(&KeyTable[Index], Key)))
             {
                IsFileDataItem = True;
                CurrentFileTabIndex = Index;
                break;
             }
             Index += 1;
          }
    The results are:

    Pass 1
    Code:
    Index: 0
    &KeyTable[Index]: "fileId"
    The 1st pass pointing to proper place

    Pass2
    Code:
    Index: 1
    &KeyTable[Index]: "ileId"
    The 2nd pass not pointing to proper place

    what is wanted in Pass 2 is the 2nd element of the string array 'fileid' (Internal name vs SQL table .schema name)
    There is an obvious disconnection between the declaration of the constant array and my 'Address of Array element'. What am I doing wrong?

    Lar

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Well the basic problem is you're lying through the cast.

    Just drop the KeyTable pointer and access the data directly.
    Code:
          while(filedata[Index][0])
          {
             if(!(strcmp(filedata[Index], Key)))
             {
                IsFileDataItem = True;
                CurrentFileTabIndex = Index;
                break;
             }
             Index += 1;
          }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 10:26 PM
  2. 2d array addressing
    By R.Stiltskin in forum C++ Programming
    Replies: 6
    Last Post: 08-19-2008, 02:36 AM
  3. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 09:21 AM
  4. constant string array
    By v6sa in forum C++ Programming
    Replies: 2
    Last Post: 05-11-2005, 05:39 AM
  5. Problem comparing string from text file with string constant
    By XenoCodex Admin in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 11:17 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21