Thread: pointer to char question

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    pointer to char question

    I am trying to cement my knowledge of pointers ,with very little success at the moment . I want to do the same with the second paragraph of code what i managed with the first . I am obviously using pointers in an illegal way .

    Can anyone shed some light.

    Code:
                    // this works
    	char word[80] = {0};
    	word[0] = 's';
    	cout << word[0] << endl;
    
    	// this doesnt
    	char* word2 = {0};
    	word2[0] = 's';
    	cout << word2[0] << endl;

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Code:
    // this doesnt
    	char* word2 = {0};
    	word2[0] = 's';
    	cout << word2[0] << endl;
    No the porper way of doing it is

    Code:
    char *word2 = "s";
    cout << word2[0] << endl;
    /*
    char *name = "Steve";
    cout << name[0] << endl; //prints S
    cout << name[1] << endl; //prints t
    cout << name[2] << endl; //prints e
    cout << name << endl; //prints Steve
    */
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things don´t come easy in life!!!

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    66
    When you say:
    char word[80] = {0};

    You are telling the compiler to allocate 80 characters in an array for you to use.

    When you say:
    char *words[80] = {0};

    You are telling the compiler to allocate 80 pointers to a char type. So in essence you have allocated space for 80 potential words, but there pointers are not pointing anywhere at the moment.

    Your array 'words' looks like this:
    Code:
    words[0] = NULL;
    words[1] = NULL;
    words[2] = NULL;
    ...
    words[79] = NULL;
    To use it you need to allocate space and assign data to it.

    For example:

    Code:
    words[0] = strdup("This");
    words[1] = strdup("is");
    words[2] = strdup("a");
    words[3] = strdup("sentence.");
    ...
    This is an array of pointers to char* which is what char *[80] allocated for you.

    When you are done, each of the lements that you allocated MUST be free'd or you will leak memory:
    Code:
    free(words[0]);
    free(words[1]);
    free(words[2]);
    free(words[3]);
    Usually this can be done in a following way:
    Code:
    for (int i=0; i<80; ++i) {
      if (words[i]) {
        delete words[i];
        words[i] = NULL;
      }
    }
    Since we are in the C++ world you should be using something akin to:
    Code:
    std::string words[80];
    words[0] = "This";
    words[1] = "is";
    words[2] = "a";
    words[3] = "sentence";
    and not worry about deallocation, as the std::string class will delete the data in its destructor when the obect is removed from scope.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    84
    i dont want to initialize a value into the char pointer there and then like this
    Code:
    char *word2 = "s";
    i want something like this
    Code:
    char* word2 = {0};      // this doesnt work
    then initialize it later.
    Can this be done?

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    66
    char *word2 is a pointer to an array of char's, so you are really doing:

    char *word2 = NULL;

    at a later time you must use new/delete to allocate/deallocate the actual string.

    Code:
    char *word2 = NULL;
    
    // some code here
    
    word2 = new char[20];
    strcpy(word2, "something");
    
    // rest of the code
    
    delete []word2;    // release memory
    word2 = NULL;     // just in case

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Basic things I need to know
    By maxorator in forum C++ Programming
    Replies: 53
    Last Post: 10-15-2006, 04:39 PM
  5. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM