CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2002
    Location
    La Plata, Buenos Aires
    Posts
    615

    Unhappy why the following code using char* doesn't work?

    why the following code doesn't work (gives me a general protection fault):

    Code:
    int    MAX = 200;
    char *optiondesc[MAX];
    
    int main()
    {
      ...
    
       // get maximum number of options
        
        do
        {
            cout << "Enter the number of options you want to evaluate:" << endl
                << "(minimum is 2)" << endl;
            cout << "?";
            
            cin >> numopt;
        } while (numopt < 2);
        
    	for (i=0; i < numopt; i++)
    	{
    		cout << "Option " << i << "?";
    		cin >> optiondesc[i];
    	}
    ...
    return 0;
    }

    May be I've not allocated spacE?

  2. #2
    Join Date
    Oct 2002
    Location
    Tx, US
    Posts
    208
    May be I've not allocated spacE?
    -----------------------------------------------------------
    YES ur right u have not allocated the space for char*
    try something like this
    for (i=0; i <MAX ; i++)
    {
    optiondesc[i]=(char*)malloc(size);

    }


    Vinod

  3. #3
    Join Date
    Jun 2002
    Posts
    1,417
    since you are using C++, why not use the vector and get rid of the malloc() stuff

    Code:
    #pragma warning(disable : 4786)
    #include <stdio.h>
    #include <string>
    #include <vector>
    #include <iostream>
    using namespace std;
    
    
    vector<string> optiondesc;
    
    int main()
    {
    
       // get maximum number of options
        string option;
    	int numopt, i;
        do
        {
            cout << "Enter the number of options you want to evaluate:" << endl
                << "(minimum is 2)" << endl;
            cout << "?";
            
            cin >> numopt;
        } while (numopt < 2);
        
    	for (i=0; i < numopt; i++)
    	{
    		cout << "Option " << i << "?";
    		cin >> option;
    		optiondesc.push_back(option);
    	}
    
    return 0;
    }

  4. #4
    Join Date
    Jan 2003
    Location
    London
    Posts
    5
    The real reason your original code didn't work was in the declaration of 'optiondesc'.

    By declaring it as:

    char *optiondesc[MAX];

    you are saying you want and array of MAX elements where each element is of type char*.

    That's why you need to inialise each element to point to some memory.
    This could also have been done as:

    char optiondesc[MAX][256];

    (note the abscence of the * in the declaration!)

    Where each element would have been statically allocated 256 bytes.

    While this is legitimate I totally agree with the other guys responses, but just thought someone should clear it up for you as to why your original code didn't work.
    All generalisations are false.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured