Need Help Understanding Pointers

closed account (91qLy60M)
Ladies and Gentlemen of Cplusplus.com, maybe you can help me with understanding pointers. I understand the basics;
1
2
3
int *p = NULL;
int num = 20;
p = #

The thing I need clarified is function parameters. I'm confused on how the loadFile parameter below accepts a string literal if it is declared char.
1
2
3
4
5
6
void loadFile(const char *filename)
{
      // .... 
}
//################ in other file ######
loadFile("datafile.txt");
First understand what is a "string literal". From MSDN:

A string literal represents a sequence of characters that together form a null-terminated string.


Also, char *array is ACTUALLY a type of array.

Read this it may help you:
http://msdn.microsoft.com/en-us/library/69ze775t.aspx

Try running this code and see how both types produce the same thing:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{

    const char awesomeness[25] = "I'm awesome!"; //This is an array
    const char *name = "Hello Dude!"; //This is also a type of array.

    std::cout << name << std::endl;
    std::cout << awesomeness << std::endl;

    return 0;
}
Last edited on
To continue on from what @Stormboy said, because it is an array, what you are doing is you are passing the address in memory of first character of the string to the function. If you were then to, say, output the string, it just iterates over the memory, outputting each character, until the character has a value of 0 (or a null terminator).
closed account (91qLy60M)
Thank you, sirs, that makes me understand now.
Topic archived. No new replies allowed.