May i know the difference between these initializations?

  • Thread starter Thread starter shermaine80
  • Start date Start date
  • Tags Tags
    Difference
Click For Summary

Discussion Overview

The discussion centers around the differences between two types of string initializations in C: using an array and using a pointer. Participants explore the implications of each approach, including memory allocation and behavior in different contexts.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes char a[] as an array that includes the string literal, while char *p is a pointer to the address storing the string literal, seeking confirmation of this understanding.
  • Another participant asserts that an array type is effectively the same as a pointer constant, noting that while p can be reassigned, a cannot be, highlighting a functional difference.
  • A later reply emphasizes the difference in size between the array and pointer, stating that sizeof(a) yields the size of the array, while sizeof(b) yields the size of the pointer.
  • One participant provides an example of dynamically copying strings, illustrating how sizeof affects the output, particularly when using a pointer.
  • Another participant points out that sizeof(char) is always 1, suggesting a more concise way to allocate memory for the string copy.
  • One participant emphasizes the importance of writing code that is both efficient and clear, discussing the balance between performance and readability.

Areas of Agreement / Disagreement

Participants express differing views on the implications of using arrays versus pointers, particularly regarding memory allocation and behavior. There is no consensus on a single correct approach, as multiple perspectives are presented.

Contextual Notes

Participants mention the need for further methods to determine string length in practical situations, indicating that assumptions about string sizes may not always hold true.

shermaine80
Messages
30
Reaction score
0
May i know the difference between

(1) char a[] = "string literal"
(2) char *p = "string literal"

char a[] is a array which will include the word "string literal"
whilw char *p is a pointer that points to a address that store the words "string literal"?

Is that correct? Please advise.
 
Technology news on Phys.org
I believe an array type is effectively the same thing as a pointer constant. (not a constant pointer!)

i.e. these two statements mean exactly the same thing

char a[] = something;
char *const a = something;


Functionally, going back to your original example, it simply means that p = a is a valid assignment, because you're allowed to change where p points, but a = p is not, because you're not allowed to change where a points.
 
Last edited:
Hurkyl said:
I believe an array type is effectively the same thing as a pointer constant. (not a constant pointer!)

i.e. these two statements mean exactly the same thing

char a[] = "something";
char* const b = "something";


Functionally, going back to your original example, it simply means that p = a is a valid assignment, because you're allowed to change where p points, but a = p is not, because you're not allowed to change where a points.
Actually there is a subtle difference between a and b.

sizeof(a) is 10 but sizeof(b) is 4 (typically, in a compiler where chars are one byte ("something" is 9 chars plus a NUL) and pointers are 4 bytes).

But in most other contexts, a and b behave identically.
 
And sizeof does make a difference!

For example, let's try to make a copy of each string dynamically.

Code:
    char a[] = "1 String Literal";
    char *a_p = malloc (sizeof (char) * sizeof (a));
    memcpy (a_p, a, sizeof (char) * sizeof (a));
    printf ("%s\n", a_p);
    free (a_p);

Perfect! It works.

Now let's make a copy of the other string.

Code:
    char *p = "2 String Literal";
    char *p_p = malloc (sizeof (char) * sizeof (p));
    memcpy (p_p, p, sizeof (char) * sizeof (p));
    printf ("%s\n", p_p);
    free (p_p);

Uh oh! It only outputs the first four characters!

Oh? But can't we just fix it by using the correct sizeof?

Code:
    char a[] = "1 String Literal";
    char *p = "2 String Literal";
    char *p_p = malloc (sizeof (char) * sizeof (a));
    memcpy (p_p, p, sizeof (char) * sizeof (a));
    printf ("%s\n", p_p);
    free (p_p);

Hurray! It works.

Now... in a real-world situation, you will not have an alternate initialization with the exact same number of characters to use a sizeof on. In that case, further methods can be used to acquire the correct size. Such as strlen under the string.h library, for example.
 
Last edited:
Sane said:
Code:
    char *a_p = malloc (sizeof (char) * sizeof (a));
By the way, sizeof(char) is always 1.

(And even if it wasn't, the correct formulation would still be char* a_p = malloc(sizeof(a));)
 
Yes, you are right. However, I was trying to be as clear as possible.

Half of programming is knowing how to write efficiently and elegantly, the other half is showing clear and concise intentions with your code. This was an example of choosing to go with the latter, readability.
 

Similar threads

Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 89 ·
3
Replies
89
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 10 ·
Replies
10
Views
6K
Replies
2
Views
3K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K