C++ crashing when attempting to assign a new value to a pointer array pointing to chars -


so hope convert uppercase characters lower case simple addition of 32 char's value, gives it's lower case equivalent.

in main declare pointer array , assign 3 words:

int main() {     char *dictionary[10];     dictionary[0] = "auto";     dictionary[1] = "car";     dictionary[2] = "door";     int arraycount = 3;     uppercase(dictionary, &arraycount); } 

in function attempt convert:

void uppercase(char *dictionary[], int *arraycount) {     cout << "in uppercase\n";     (int = 0; < 3; i++)         (int k = 0; dictionary[i][k] != '\0'; k++)         {             if (dictionary[i][k] < 97 && dictionary[i][k] > 64) // if     capital less value of 'a' (97)             {                 dictionary[i][k] += 32;             }             else                 cout << "not capital\n";         } } 

the program crashes @ dictionary[i][k] += 32; attempting replace character in string(which pointed elements of array). converting letter first not work. ex: dictionary[i][k] = 'a'; still crashes program. running eclipse c++ on windows

most of covered in comments now. looked. yup. got busy before post. anyway, here's break-down of problem:

char *dictionary[10]; 

this cool.

dictionary[0] = "auto"; 

this not cool. "auto" string literal. string of constant values. how stored compiler. can read it, should not count on being able write it. treat though defined const char * because, far language concerned, const char *.

why not cool op assigns const char * char *. assigning pointer constant pointer non constant should generate @ least warning. best turn warning level in compiler catch sort of error early. in g++ , similar, -wall, -wextra, , side order of -pedantic. in msvc, navigate properties->c/c++->general , play warning level. enableallwarnings looks place start.

now constant values referenced non constant pointer, compiler has no clue next bit fatal.

dictionary[i][k] += 32; 

attempts add 32 character, part fine, , store result non-writable location. not allowed, exact handling of attempting impossible compiler. got program crash, quite nice of compiler. program have kept running, smashed other memory space, , died later, giving no clue happened , debug.

how make strings not constant:

  1. use std::string rather char *. in c++ far better option. , while you're @ it, use std::vector instead of array.
  2. but smells homework , may not allowed use std::string. in case, allocate storage , copy string literals storage have real, modifiable memory backing them.

coding style notes:

rather using numeric values 97, use character 'a'. works same , intent easier determine. better save trouble , use std::tolower.

there neat trick can std::transform, std::string, , std::tolower remove bulk of uppercase function. experiment bit , you'll find it. correction (and seem catch myself on one): use tolower, not std::tolower because std::tolower's locale overload makes ambiguous std::tolower want.

this:

for (int = 0; < 3; i++) 

is kind of silly. passed in arraycount. might use , save confusion if ever change number of items in array

for (int = 0; < *arraycount; i++) 

Comments