Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to a string in C?

Tags:

c

string

pointers

char *ptrChar;

I know that ptrChar is a pointer to char. What's the command for a pointer to string?

Edit: If it's the same (ptr to char vs. ptr to string) — what does the variable definition below represent?

char (*ptr)[N];

I'm confused.

like image 421
user1980750 Avatar asked Mar 01 '13 23:03

user1980750


People also ask

What is a pointer to a 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.

How do you assign a pointer to a string?

Creating a pointer for the string The variable name of the string str holds the address of the first element of the array i.e., it points at the starting memory address. So, we can create a character pointer ptr and store the address of the string str variable in it. This way, ptr will point at the string str.

How do you access string through pointers?

Accessing string using pointer Using char* (character pointer), we can access the string.

Can char * point to a string?

In C, a string is an array of characters that end in a null (0). So a char * doesn't contain a string, but it can be the address of or pointer to a string.


1 Answers

The same notation is used for pointing at a single character or the first character of a null-terminated string:

char c = 'Z';
char a[] = "Hello world";

char *ptr1 = &c;
char *ptr2 = a;      // Points to the 'H' of "Hello world"
char *ptr3 = &a[0];  // Also points to the 'H' of "Hello world"
char *ptr4 = &a[6];  // Points to the 'w' of "world"
char *ptr5 = a + 6;  // Also points to the 'w' of "world"

The values in ptr2 and ptr3 are the same; so are the values in ptr4 and ptr5. If you're going to treat some data as a string, it is important to make sure it is null terminated, and that you know how much space there is for you to use. Many problems are caused by not understanding what space is available and not knowing whether the string was properly null terminated.

Note that all the pointers above can be dereferenced as if they were an array:

 *ptr1    == 'Z'
  ptr1[0] == 'Z'

 *ptr2    == 'H'
  ptr2[0] == 'H'
  ptr2[4] == 'o'

 *ptr4    == 'w'
  ptr4[0] == 'w'
  ptr4[4] == 'd'

  ptr5[0] ==   ptr3[6]
*(ptr5+0) == *(ptr3+6)

Late addition to question

What does char (*ptr)[N]; represent?

This is a more complex beastie altogether. It is a pointer to an array of N characters. The type is quite different; the way it is used is quite different; the size of the object pointed to is quite different.

char (*ptr)[12] = &a;

(*ptr)[0] == 'H'
(*ptr)[6] == 'w'

*(*ptr + 6) == 'w'

Note that ptr + 1 points to undefined territory, but points 'one array of 12 bytes' beyond the start of a. Given a slightly different scenario:

char b[3][12] = { "Hello world", "Farewell", "Au revoir" };

char (*pb)[12] = &b[0];

Now:

(*(pb+0))[0] == 'H'
(*(pb+1))[0] == 'F'
(*(pb+2))[5] == 'v'

You probably won't come across pointers to arrays except by accident for quite some time; I've used them a few times in the last 25 years, but so few that I can count the occasions on the fingers of one hand (and several of those have been answering questions on Stack Overflow). Beyond knowing that they exist, that they are the result of taking the address of an array, and that you probably didn't want it, you don't really need to know more about pointers to arrays.

like image 116
Jonathan Leffler Avatar answered Oct 15 '22 00:10

Jonathan Leffler