May i know the difference between these initializations?

  • Thread starter shermaine80
  • Start date
  • Tags
    Difference
In summary, the difference between char a[] and char *p is that char a[] is an array that contains the string literal, while char *p is a pointer that points to an address that stores the string literal. This means that p = a is a valid assignment, but a = p is not. Additionally, there is a subtle difference between a and b, as sizeof(a) is 10 and sizeof(b) is 4. This can affect how strings are copied dynamically. However, in a real-world situation, methods such as strlen can be used to acquire the correct size.
  • #1
shermaine80
30
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
  • #2
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:
  • #3
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.
 
  • #4
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:
  • #5
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));)
 
  • #6
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.
 

1. What is an initialization in scientific research?

An initialization in scientific research refers to the process of setting up the initial conditions or parameters for an experiment or study. It is the first step in the scientific method and is crucial for ensuring accurate and reliable results.

2. What is the difference between an initialization and a hypothesis?

An initialization is the starting point of an experiment, while a hypothesis is a proposed explanation for a phenomenon that is being tested through the experiment. An initialization sets the conditions for the experiment, while a hypothesis predicts the outcome based on those conditions.

3. How do initializations impact the results of a study?

Initializations play a crucial role in determining the accuracy and reliability of the results of a study. The initial conditions set up in an experiment can greatly influence the outcomes, and it is important to carefully consider and control these factors to ensure the validity of the results.

4. Are there different types of initializations in scientific research?

Yes, there are different types of initializations depending on the type of study or experiment being conducted. Some examples include randomization of participants, control groups, and standardization of equipment and procedures. The type of initialization used will depend on the specific research question and design of the study.

5. How do scientists ensure the validity of their initializations?

Scientists use various methods to ensure the validity of their initializations, such as conducting pilot studies, using standardized protocols, and double-checking equipment and procedures. It is also important for scientists to clearly document their initializations and any changes made throughout the study to maintain transparency and reproducibility.

Similar threads

  • Programming and Computer Science
Replies
5
Views
884
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
944
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top