Hey, Is there any difference between the 2 syntax,
ie, str = "This is a test"; and strcpy(str,"This is a test");

I think, both of them, malloc some random 200 addresses, and give the starting location of the 200 chunk to the pointer variable 'ptr'.

#include<stdio.h>

char* str;

int main(void)
{
 str = (char*)malloc(200 * sizeof(char));

 //str = "This is a test";
 strcpy(str,"This is a test");

 printf("\nAddress = %u",str);
 printf("\nContents = %s",str);

 return 0;
}

Dani AI

Generated

Short answer: the two forms are not the same. and got the essentials right — one makes a pointer refer to static storage (a literal), the other copies bytes into storage you must provide. Below are practical clarifications and safety tips that are not covered in the thread.

Do not try to print pointer values with %u; use %p and cast to void *. Do not call free() on a pointer that was set to a string literal. When you want a dynamically owned copy, allocate exactly strlen(src) + 1 bytes, check the result of malloc, then copy and free when done. strdup (where available) does the allocate+copy for you. Prefer const char * for pointers to literals so accidental modification attempts are caught by the compiler.

Example pattern (safe allocation and printing):

const char *src = "This is a test";
size_t len = strlen(src) + 1;
char *copy = malloc(len); /* no cast in C */
if (copy) {
    memcpy(copy, src, len);
    printf("Address = %p\nContents = %s\n", (void*)copy, copy);
    free(copy);
}

Other gotchas: char arr[] = "..." creates a modifiable array with automatic or static storage depending on where it is declared, while char *p = "..." makes p point at static storage (no allocation). sizeof gives different results: sizeof arr yields the array size (including '\0'), but sizeof p returns the pointer size. Identical literals may be merged by the compiler, so two pointers to the same literal can share the same address. Finally, avoid strcpy into a buffer unless you are sure the buffer is large enough; prefer snprintf, strlcpy (where available), or explicit length-checked copies.

Recommended Answers

All 3 Replies

Yes there is a difference between the two

str = "Test", the pointer just simply points to a string literal that resides in read-only memory and whose memory was allocated by the compiler when the program was compiled. The compiler reserves a whole block of memory in your program for all the string literals. You don't have to do anything specific for this. Because it's in read-only memory it's contents cannot be changed.

strcpy(str,"Test") -- with this one you will have to allocate memory for str before you can call strcpy(). Two ways to do it

  1. static allocation, such as char str[255];
  2. dynamic alllocation such as using malloc()

Ancient Dragon is correct, but I'd like to point out that there is a difference between

char *str = "Test";
/* and */
char str[] = "Test";

In both cases, "Test" is a string that resides in read-only memory. In the first case, str is a pointer object that is initialized to the address of the read-only string, as Ancient Dragon described.

But in the second case, str is an array object, and the initialization actually copies the string from read-only memory into the new (read-write) array. This memory has automatic storage duration, so it disappears when str goes out of scope -- you don't have to worry about free()ing it like you do with malloc().

As an aside, this kind of automatic copying is possible only with initialization, not ordinary assignment, so the following is not valid:

char str[100];
str = "Test";

This is because an array name is not an lvalue. The other version,

char *str;
str = "Test";

is more or less fine, but you must be very careful if you ever modify the string because str points to read-only memory. If you don't try to modify the string, declare it as char const *str; instead.

commented: good points :) +0
commented: Good points indeed! +13

Thanx Ancient Dragon and Trentacle for your good explanation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.