Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a string literal as a function parameter defined as a pointer

Tags:

c

pointers

I am reading the chapter on arrays and pointers in Kernighan and Richie's The C Programming Language.

They give the example:

/* strlen:  return length of string s */
int strlen(char *s)
{
    int n;

    for (n = 0; *s != '\0'; s++)
        n++;
    return n;
}

And then say:

“Since s is a pointer, incrementing it is perfectly legal; s++ has no effect on the character string in the function that called strlen, but merely increments strlen’s private copy of the pointer. That means that calls like

strlen("hello, world");  /* string constant */
strlen(array);           /* char array[100]; */
strlen(ptr);             /* char *ptr; */

all work.”

I feel like I understand all of this except the first call example: Why, or how, is the string literal "hello, world" treated as a char *s? How is this a pointer? Does the function assign this string literal as the value of its local variable *s and then use s as the array name/pointer?

like image 444
Dmitry Minkovsky Avatar asked Jan 21 '13 19:01

Dmitry Minkovsky


People also ask

How do I pass a string to a pointer to a function?

In C, if you need to amend a string in a called function, pass a pointer to the first char in the string as an argument to the function. If you have allocated storage for the string outside the function, which you cannot exceed within the function, it's probably a good idea to pass in the size.

Is a string literal a pointer?

String literals are passed to functions as pointers to a stored string. For example, given the statement: printf( "Please enter a positive value for the angle: " );

How do you pass a string as a parameter in C++?

First, we have the function definition “DisplayString,” where a constant string reference is passed. The constant strings are defined and initialized in the main function as “str1” and “str2”. After that, pass these constant strings to the function “InputString”.

Are strings passed by reference in C?

strings are passed by reference. The built in string type is a value type.


2 Answers

To understand how a string like "Hello World" is converted to a pointer, it is important to understand that, the string is actually hexadecimal data starting at an address and moving along till it finds a NULL

So that means, every string constant such as "Hello World" is stored in the memory somewhere

Possibility would be:

0x10203040 : 0x48 [H]
0x10203041 : 0x65 [e]
0x10203042 : 0x6C [l]
0x10203043 : 0x6C [l]
0x10203044 : 0x6F [o]
0x10203045 : 0x20 [' ']
0x10203046 : 0x57 [W]
0x10203047 : 0x6F [o]
0x10203048 : 0x72 [r]
0x10203049 : 0x6C [l]
0x1020304A : 0x64 [d]
0x1020304B : 0x00 [\0]

So, when this function is called with the above values in the memory, [left side is address followed by ':' and the right side is ascii value of the character]

int strlen(const char *s)
{
    int n;

    for (n = 0; *s != ′\0′; s++)
        n++;
    return n;
}

strlen("Hello World");

at that time, what gets passed to strlen is the value 0x10203040 which is the address of the first element of the character array.

Notice, the address is passed by value.. hence, strlen has its own copy of the address of "Hello World". starting from n = 0, following uptil I find \0 in the memory, I increment n and also the address in s(which then gets incremented to 0x10203041) and so on, until it finds \0 at the address 0x1020304B and returns the string length.

like image 72
Aniket Inge Avatar answered Oct 21 '22 12:10

Aniket Inge


"hello, world"

is an array of char (type is char[13]). The value of an array of char in an expression is a pointer to char. The pointer points to the first element of the array (i.e., the value of "hello, world" is &"hello, world"[0]).

like image 38
ouah Avatar answered Oct 21 '22 10:10

ouah