what exactly is char **??
how is it used in getline function?

Recommended Answers

All 4 Replies

It's a pointer that points to a pointer that points to a char

As for how it works, work it out. Draw a map of memory and see how it could possibly be layed out and used.

think of this like this:

#include <iostream>
using namespace std;

int main(){
    char a='a';
    cout<<"Char a: "<<a<<endl;
    char* p=new char(a);
    cout<<"Char* p(points to a): "<<*p<<endl;
    char** pp=new char*(p);
    cout<<"Char** pp (points to p which points to a): "<<**pp<<endl;
    return (0);
}

Also you can do a 2x2 matrix out of a char**:

#include <iostream>
using namespace std;

#define row 3
#define col 3

char** CreateCharMatrix(){
    char ** matrix;
    matrix=new char*[row];
    for (int i=0;i<row;i++){
        matrix[i]=new char[col];
    }
    return (matrix);
}

void freeMemory(char** matrix){
    for (int i=0;i<row;i++){
        delete [] matrix[i];
    }
    delete [] matrix;
}

int main(){
    char ** matrix;
    matrix=CreateCharMatrix();
    for (int i=0;i<row;i++)
        for (int j=0;j<col;j++)
            matrix[i][j]=char('a'+j);
    for (int i=0;i<row;i++)
        for (int j=0;j<col;j++){
            cout<<matrix[i][j];
            if (j+1==3) cout<<'\n';
        }
    freeMemory(matrix);
    return (0);
}

Don't forget, when you're using memory in a dynamical way, you have to deallocate it manually after you're done.

thankx i got this..
but i have one more problem....what is a string exactly??
becoz getline(char **,size_t,FILE)
it works as getline(cin,data); //where data is a string i.e. string data;
however somehow i think string is not exactly char **.

a char* is considered a c-style string. It is just an array of characters. A char ** is and array of char* which is to say it is an group of c-style strings. The string type is an ogject created from the class string. These are two different things. This is a good page for learning about char arrays http://www.cplusplus.com/doc/tutorial/ntcs/
this is a goo page to learn about the string class http://www.cplusplus.com/reference/string/string/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.