GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Sep-2007, 23:34
rag84dec rag84dec is offline
New Member
 
Join Date: Jun 2007
Posts: 11
rag84dec is an unknown quantity at this point

Problem with the pointer...please help


CPP / C++ / C Code:
#include<iostream>
using namespace std;
void f(const int);
void f(int);
void f(const int t)
{
    cout<< "value id : "<<t;
}
void main()
{

    f(1);
    char **p;
    *p=new  char [5];
    
}
Last edited by admin II : 14-Sep-2007 at 04:46. Reason: Please surround your C++ code with [CPP] your code [/CPP]
  #2  
Old 13-Sep-2007, 23:38
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: Problem with the pointer...please help


What's the problem? Explain it. In English. Or Finnish. How do the functions relate to the pointer in any way? I just can't see the connection.
  #3  
Old 14-Sep-2007, 02:04
rag84dec rag84dec is offline
New Member
 
Join Date: Jun 2007
Posts: 11
rag84dec is an unknown quantity at this point

Re: Problem with the pointer...please help


OK sorry....wen i execute the program i get no compiler errors but a "run time " exception saying "variable p has been used without being defined"


please,tell me reason??...
  #4  
Old 14-Sep-2007, 05:45
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: Problem with the pointer...please help


Quote:
Originally Posted by rag84dec
OK sorry....wen i execute the program i get no compiler errors but a "run time " exception saying "variable p has been used without being defined"


please,tell me reason??...
Well okay, first of all, I'm not good at explaining stuff and second, I never use pointers to pointers or that kind of stuff if I can avoid it. And I usually avoid it with pointers to structures containing pointers to something. It just makes things easier for me to understand.

But hopefully this helps.

CPP / C++ / C Code:
char **p;
*p=new  char [5];
p is a pointer to pointer to char. In that case, *p is a pointer to char and **p is the char itself. Here in your example you try to dereference the pointer to char *p, but it has not been initialized yet.

Here's a simple example:
CPP / C++ / C Code:
char **p;
p = new char*;
*p = new char[5];
After this the pointer to char, *p points to a valid location belonging to your program.

As I said, I can't explain it really, but hopefully this little example helps:
CPP / C++ / C Code:
#include<iostream>

int main()
{
    using std::cout;
    using std::endl;

    const int LINES = 3;
    const int COLUMNS = 5;

    char **p;                               // Pointer to pointer to char
    p = new char*[LINES];                   // Pointer to pointer to char gets space for pointers to char

    for (int i = 0; i < LINES; ++i)
    {
        p[i] = new char[COLUMNS];           // Each pointer to char gets space for chars
    }

    for (int i = 0; i < LINES; ++i)
    {
        for (int j = 0; j < COLUMNS; ++j)
        {
            p[i][j] = 'k';                  // Assign the chars
//            *(*(p + i) + j) = 'k';        // Same as above. I think..
        }
    }

    for (int i = 0; i < LINES; ++i)
    {
        for (int j = 0; j < COLUMNS; ++j)
        {
            cout << p[i][j] << " ";         // Print the chars
        }
        cout << endl;
    }

    cout << **p << endl;                    // Same as p[0][0]

    // And here the good programmer would go through deleting the memory...

    return 0;

}
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
MACRO to detect big / little endian ahbi82 C Programming Language 14 26-Aug-2007 10:33
Pointer Problem Esam CPP / C++ Forum 1 22-May-2006 15:39
Pointer Usage in C++: Beginner to Advanced varunhome CPP / C++ Forum 0 19-Aug-2005 09:25
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36
Please help! Newbie pointer problem! robsmith C Programming Language 5 12-Mar-2005 04:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:51.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.