5

So...say I had a function like this...

int function( const char *c )
{
 //do something with the char *c here...
}

what does char *c mean? I know about chars in general I think but I don't get what the * does...or how it changes the meaning.

Sadique
  • 22,572
  • 7
  • 65
  • 91
JacKeown
  • 2,780
  • 7
  • 26
  • 34
  • 2
    It's a pointer. You need to learn this from a book or a lecture, it's not something that can be explained in a few lines if you don't already understand it. –  Apr 19 '11 at 01:47
  • Possible duplicate of [How do pointer to pointers work in C?](http://stackoverflow.com/questions/897366/how-do-pointer-to-pointers-work-in-c) – phuclv May 03 '17 at 03:00

9 Answers9

6

It means that this is a pointer to data of type char.

Todd Hopkinson
  • 6,803
  • 5
  • 32
  • 34
  • 1
    "Pointer of type char" is unusual terminology, and perhaps a bit confusing. "Pointer to char", or even "pointer of type pointer-to-char" would be better. – Thomas Padron-McCarthy Apr 19 '11 at 19:35
6

char *c means that c is a pointer. The value that c points to is a character.

So you can say char a = *c.

const on the other hand in this example says that the value c points to cannot be changed. So you can say c = &a, but you cannot say *c = 'x'. If you want a const pointer to a const character you would have to say const char* const c.

spockaroo
  • 188
  • 1
  • 5
2

This is a pointer to a character. You might want to read up about pointers in C, there are about a bazillion pages out there to help you do that. For example, http://boredzo.org/pointers/.

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
sjr
  • 9,769
  • 1
  • 25
  • 36
2

Pointer to a char. That is, it holds the address at which a char is located.

splonk
  • 1,045
  • 6
  • 11
2

Thats a pointer-to-char. Now that you know this, you should read this:

About character pointers in C

Community
  • 1
  • 1
Sadique
  • 22,572
  • 7
  • 65
  • 91
1

It means the argument should be a pointer to a character.

You would dereference it with * as well.

alex
  • 479,566
  • 201
  • 878
  • 984
1

You might want to read Const correctness page to get a good idea on pointer and const.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
1

http://cslibrary.stanford.edu/ is the best resource that I have come across to learn about pointers in C . Read all the pointer related pdfs and also watch the binky pointer video.

deovrat singh
  • 1,220
  • 2
  • 17
  • 33
1

This is a pointer to a char type. For example, this function can take the address of a char and modify the char, or a copy of a pointer, which points to an string. Here's what I mean:

char c = 'a';
f( &c );

this passes the address of c so that the function will be able to change the c char.

char* str = "some string";
f( str );

This passes "some string" to f, but f cannot modify str.

It's a really basic thing for c++, that higher-level languages (such as Java or Python) don't have.

Pooh
  • 21
  • 3