About pointers and strcpy () in C

I am doing memory allocation using malloc()

with pointers, but 1 observation of pointers is such that it strcpy()

can accept a variable str

without *

:

char *str;
str = (char *) malloc(15);
strcpy(str, "Hello");
printf("String = %s,  Address = %u\n", str, str);

      

But with integers, we need * to give str a value.

int *str;
str = (int *) malloc(15);
*str = 10;
printf("Int = %d,  Address = %u\n", *str, str);

      

it really confuses me why strcpy()

accepts str

, because in my own understanding "Hello"

will be passed to a memory location str

, leading to some errors.

+3


source to share


5 answers


In C, a string is (by definition) an array of characters. However (whether we implement this all the time or not), we almost always access arrays using pointers. Thus, while C does not have a true "string" type, for most practical purposes this function is used for a pointer of type-to-char (ie char *

). Virtually any function that takes or returns a string will use char *

. Therefore, strlen()

they strcpy()

accept char *

. Therefore, it printf %s

expects char *

. In all these cases, these functions are needed - a pointer to the first character of the string. (They then read the rest of the line sequentially, stopping when they find a trailing character '\0'

.)

In these cases, you are not using an explicit symbol *

. *

will only extract the character it points to (i.e. the first character of the string), but you don't want to extract the first character, you want to pass the entire string (i.e. a pointer to an integer string) up strcpy

to so that it can do its job.



In your second example, you weren't working with a string at all. (The fact that you used a variable with a name str

confused me for a moment.) You have a pointer to some ints, and you are working with the first int that it points to. Because you are referring directly to one of the things that indicates why you need an explicit symbol *

.

+4


source


*

called indirect or dereference mode.

In your second code

 *str = 10;

      

assigns the value 10 to the memory address specified str

. This is one value (i.e. one variable).



OTOTH, strcpy()

copies the entire line at a time. It takes two parameters char *

, so you don't need *

to dereference to get the value when passing arguments.

You can use the non-dereferencing operator by strcpy()

copying item by item like

char *str;
str = (char *) malloc(15);   //success check TODO
int len = strlen("Hello");    //need string.h header

for (i = 0; i < len; i ++)
    *(str+i)= "Hello"[i];  // the * form. as you wanted

str[i] = 0;   //null termination

      

+1


source


Many string manipulation functions, including strcpy

, by convention and design, accept a pointer to the first character of an array rather than a pointer to the entire array, although their values ​​are the same.

This is because the types are different; for example a pointer to char[10]

is of a different type from a pointer to char[15]

, and walking around a pointer to the entire array would be impossible or very awkward because of this, unless you spell them all over the place or create different functions for different lengths.

For this reason, they have established a passing convention around the string with a pointer to its first character rather than the entire array, possibly with its length when needed. Many functions that work with an array, such as memset

, work the same way.

0


source


Well, here's what happens in the first snippet:

First, you dynamically allocate 15 bytes of memory by storing that address to a char pointer, which is a pointer to a 1-byte data sequence (string). Then you call strcpy()

, which iterates through the string and copies the characters, byte by byte, into the newly allocated memory space. Each character is a number based on an ASCII table, for example. symbol a = 97 (take a look at man ascii

).

Then you pass that address in printf()

, which reads from the string, a byte per byte, and then flushes it to your terminal.

In the second snippet, the process is the same, you still allocate 15 bytes, storing the address in an int * pointer. Int is a 4 byte data type. When you execute *str = 10

, you are casting a pointer to store the value 10 at the address pointed to by str. Remind me what I wrote earlier, you could have done * str = 'a' and that integer index 0 would have the value 97 even if you tried to read it as an int. you can print it if you like.

So why strcpy()

could it take a parameter int *

like? Because this is the memory space where it can write, byte for byte. You can store "Hell" in int, then "o!" in the following.

It's just all about usability.

0


source


See if there is a difference between operator =

and function strcpy

.

*

- postulate operator. When you say *str

it means the value in the memory location pointed to by str.

Also as good practice use this

str = (char *) malloc( sizeof(char)*15 )

      

This is because the size of the datatype can be different on different platforms. Hence, use a function sizeof

to determine its actual size at run time.

0


source







All Articles