Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C style strings, Pointers, arrays

Tags:

People also ask

What is array of pointers to string in C?

Pointer to string in C can be used to point to the starting address of the array, the first character in the array. These pointers can be dereferenced using the asterisk * operator to identify the character stored at the location. 2D arrays and pointer variables both can b used to store multiple strings.

What is the C-style character string?

A C-style string is simply an array of characters that uses a null terminator. A null terminator is a special character ('\0', ascii code 0) used to indicate the end of the string. More generically, A C-style string is called a null-terminated string.

Are strings character arrays in C?

Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character '\0'.

What does C-style mean?

A C-Style array is just a "naked" array - that is, an array that's not wrapped in a class, like this: char[] array = {'a', 'b', 'c', '\0'}; Or a pointer if you use it as an array: Thing* t = new Thing[size]; t[someindex].


I'm having trouble understanding what a C-style string is. Happy early New Year

What I know: A pointer holds a memory address. Dereferencing the pointer will give you the data at that memory location.

int x = 50;
int* ptr = &x;    //pointer to an integer, holds memory address of x

cout << "&x: " << &x << endl;  //these two lines give the same output as expected
cout << "ptr: " << ptr << endl;

cout << "*ptr: " << dec << (*ptr) << endl;  //prints out decimal number 50
                                           //added dec, so the program doesnt 
                //continue to printout hexidecimal numbers like it did for the 
                 //the memory addresses above
cout << "&ptr: " << &ptr << endl;  //shows that a pointer, like any variable,
                                  //has its own memory address

Now to what I don't understand (using what's above as the source for my confusion): There are multiple ways to declare strings. I'm learning C++, but you can also use C-style strings (good to understand, although inferior to C++ strings)

C++:

string intro = "Hello world!"; 
//the compiler will automatically add a null character, \0, so you don't have to
//worry about declaring an array and putting a line into it that is bigger than 
//it can hold. 

C-style:

char version1[7] = {'H','i',' ','y','o','u','\0'};
char version2[] = "Hi you"; //using quotes, don't need null character? added for you?
char* version3 = "Hi you";

Version3 is where I'm having trouble. Here, there is a pointer to a char. I know that an array name is a pointer to the first element in an array.

cout << " &version3: " << &version3 << endl; //prints out location of 'H'
cout << " *version3: " << *version3 << endl; //prints out 'H'
cout << "  version3: " <<  version3 << endl; //prints out the whole string up to
                                             //automatically inserted \0

Before, in the section "what I knew," printing out the name of a pointer would print out the address that it held. Here, printing out the name of a pointer prints out the whole string. Do the double quotes around "Hi you" somehow tell the program: "hey I know you are a pointer, and you are initialized to the location of 'H', but because I see these double quotes, skip forward 1 byte in memory location and printout everything you see until you reach a \0" (1 byte movement because char's are 1 byte large).

How is it that printing out a pointer prints out a string? Before printing out a pointer name printed out the memory address it was initialized to.

Edit: Does cout << &version3 print out the location of 'H' or the location of the pointer, version3, which holds the memory address of 'H'?