Skip to main content
added 4 characters in body; edited tags
Source Link
Yu Hao
  • 121.5k
  • 47
  • 242
  • 301

I am having a little bit of trouble understanding the use of pointers. Specifically, I am confused about the following line of code:

char* s = "my String";

I have two questions:

  1. Is this code even legitimate? From watching some of the Harvard CS 50 videos, I would think that it is.

  2. If the answer to (1) is yes, then I am quite confused. I thought the meaning of the statement: "char* s" is that you want to create a new variable called s that is of type char*. This means that s is a pointer to some char variable (and at this point, we don't even know what this char variable is). If this is the case, then the right hand side of the above line of code should be an address in memory, and not a string.

I would be more comfortable with something like:

char s[10] = "my string"; //10 because including extra space for null 0  
char* AddressOfs = &s;

EDIT/UPDATE: Thanks for all the great answers. David K's answer has made things much clearer (I like the step-by-step structure), but I am still a bit confused in some respects.

  1. In step 1 of David K's answer, he said that

    It will reserve 10 bytes of memory somewhere.

But it has to reserve more than just 10 bytes, right? We need 10 bytes for the contents of the string plus 4 bytes for the contents of the address in memory.

  1. Do you agree with the following: So if we stick with the "variables-as-containers" analogy, the line

    char* s = "my string";

     char* s = "my string";
    

reserves 14 bytes of storage (10 for the contents of the string and 4 for the address of the contents of the string), but there is only one "container". This container is named s and holds the 4 bytes. The 10 bytes that hold the contents of the string are "container-less", i.e. these 10 bytes are allocated somewhere in memory, but there is no label that we can associate with these 10 bytes. (I'm not saying we have no way of accessing these 10 bytes. I'm just saying that there is no formal variable label that is being applied to these 10 bytes.)

I am having a little bit of trouble understanding the use of pointers. Specifically, I am confused about the following line of code:

char* s = "my String";

I have two questions:

  1. Is this code even legitimate? From watching some of the Harvard CS 50 videos, I would think that it is.

  2. If the answer to (1) is yes, then I am quite confused. I thought the meaning of the statement: "char* s" is that you want to create a new variable called s that is of type char*. This means that s is a pointer to some char variable (and at this point, we don't even know what this char variable is). If this is the case, then the right hand side of the above line of code should be an address in memory, and not a string.

I would be more comfortable with something like:

char s[10] = "my string"; //10 because including extra space for null 0  
char* AddressOfs = &s;

EDIT/UPDATE: Thanks for all the great answers. David K's answer has made things much clearer (I like the step-by-step structure), but I am still a bit confused in some respects.

  1. In step 1 of David K's answer, he said that

    It will reserve 10 bytes of memory somewhere.

But it has to reserve more than just 10 bytes, right? We need 10 bytes for the contents of the string plus 4 bytes for the contents of the address in memory.

  1. Do you agree with the following: So if we stick with the "variables-as-containers" analogy, the line

    char* s = "my string";

reserves 14 bytes of storage (10 for the contents of the string and 4 for the address of the contents of the string), but there is only one "container". This container is named s and holds the 4 bytes. The 10 bytes that hold the contents of the string are "container-less", i.e. these 10 bytes are allocated somewhere in memory, but there is no label that we can associate with these 10 bytes. (I'm not saying we have no way of accessing these 10 bytes. I'm just saying that there is no formal variable label that is being applied to these 10 bytes.)

I am having a little bit of trouble understanding the use of pointers. Specifically, I am confused about the following line of code:

char* s = "my String";

I have two questions:

  1. Is this code even legitimate? From watching some of the Harvard CS 50 videos, I would think that it is.

  2. If the answer to (1) is yes, then I am quite confused. I thought the meaning of the statement: "char* s" is that you want to create a new variable called s that is of type char*. This means that s is a pointer to some char variable (and at this point, we don't even know what this char variable is). If this is the case, then the right hand side of the above line of code should be an address in memory, and not a string.

I would be more comfortable with something like:

char s[10] = "my string"; //10 because including extra space for null 0  
char* AddressOfs = &s;

EDIT/UPDATE: Thanks for all the great answers. David K's answer has made things much clearer (I like the step-by-step structure), but I am still a bit confused in some respects.

  1. In step 1 of David K's answer, he said that

    It will reserve 10 bytes of memory somewhere.

But it has to reserve more than just 10 bytes, right? We need 10 bytes for the contents of the string plus 4 bytes for the contents of the address in memory.

  1. Do you agree with the following: So if we stick with the "variables-as-containers" analogy, the line

     char* s = "my string";
    

reserves 14 bytes of storage (10 for the contents of the string and 4 for the address of the contents of the string), but there is only one "container". This container is named s and holds the 4 bytes. The 10 bytes that hold the contents of the string are "container-less", i.e. these 10 bytes are allocated somewhere in memory, but there is no label that we can associate with these 10 bytes. (I'm not saying we have no way of accessing these 10 bytes. I'm just saying that there is no formal variable label that is being applied to these 10 bytes.)

added 1164 characters in body
Source Link
Sam Y.
  • 111
  • 4

I am having a little bit of trouble understanding the use of pointers, specifically. Specifically, I am confused about the following line of code:

char* s = "my String";

I have two questions:I have two questions:

  1. Is this code even legitimate? From watching some of the Harvard CS 50 videos, I would think that it is.

  2. If the answer to (1) is yes, then I am quite confused. I thought the meaning of the statement: "char* s" is that you want to create a new variable called s that is of type char*. This means that s is a pointer to some char variable (and at this point, we don't even know what this char variable is). If this is the case, then the right hand side of the above line of code should be an address in memory, and not a string.

I would be more comfortable with something like:

char s[10] = "my string"; //10 because including extra space for null 0  
char* AddressOfs = &s;

EDIT/UPDATE: Thanks for all the great answers. David K's answer has made things much clearer (I like the step-by-step structure), but I am still a bit confused in some respects.

  1. In step 1 of David K's answer, he said that

    It will reserve 10 bytes of memory somewhere.

But it has to reserve more than just 10 bytes, right? We need 10 bytes for the contents of the string plus 4 bytes for the contents of the address in memory.

  1. Do you agree with the following: So if we stick with the "variables-as-containers" analogy, the line

    char* s = "my string";

reserves 14 bytes of storage (10 for the contents of the string and 4 for the address of the contents of the string), but there is only one "container". This container is named s and holds the 4 bytes. The 10 bytes that hold the contents of the string are "container-less", i.e. these 10 bytes are allocated somewhere in memory, but there is no label that we can associate with these 10 bytes. (I'm not saying we have no way of accessing these 10 bytes. I'm just saying that there is no formal variable label that is being applied to these 10 bytes.)

I am having a little bit of trouble understanding the use of pointers, specifically, I am confused about the following line of code:

char* s = "my String";

I have two questions:

  1. Is this code even legitimate? From some of the CS 50 videos, I would think that it is.

  2. If the answer to (1) is yes, then I am quite confused. I thought the meaning of the statement: "char* s" is that you want to create a new variable called s that is of type char*. This means that s is a pointer to some char variable (and at this point, we don't even know what this char variable is). If this is the case, then the right hand side of the above line of code should be an address in memory, and not a string.

I would be more comfortable with something like:

char s[10] = "my string"; //10 because including extra space for null 0  
char* AddressOfs = &s;

I am having a little bit of trouble understanding the use of pointers. Specifically, I am confused about the following line of code:

char* s = "my String";

I have two questions:

  1. Is this code even legitimate? From watching some of the Harvard CS 50 videos, I would think that it is.

  2. If the answer to (1) is yes, then I am quite confused. I thought the meaning of the statement: "char* s" is that you want to create a new variable called s that is of type char*. This means that s is a pointer to some char variable (and at this point, we don't even know what this char variable is). If this is the case, then the right hand side of the above line of code should be an address in memory, and not a string.

I would be more comfortable with something like:

char s[10] = "my string"; //10 because including extra space for null 0  
char* AddressOfs = &s;

EDIT/UPDATE: Thanks for all the great answers. David K's answer has made things much clearer (I like the step-by-step structure), but I am still a bit confused in some respects.

  1. In step 1 of David K's answer, he said that

    It will reserve 10 bytes of memory somewhere.

But it has to reserve more than just 10 bytes, right? We need 10 bytes for the contents of the string plus 4 bytes for the contents of the address in memory.

  1. Do you agree with the following: So if we stick with the "variables-as-containers" analogy, the line

    char* s = "my string";

reserves 14 bytes of storage (10 for the contents of the string and 4 for the address of the contents of the string), but there is only one "container". This container is named s and holds the 4 bytes. The 10 bytes that hold the contents of the string are "container-less", i.e. these 10 bytes are allocated somewhere in memory, but there is no label that we can associate with these 10 bytes. (I'm not saying we have no way of accessing these 10 bytes. I'm just saying that there is no formal variable label that is being applied to these 10 bytes.)

improved formatting
Source Link
Sourav Ghosh
  • 133.9k
  • 16
  • 187
  • 266

I don't have really any formal training as a computer scientist (I was trained as an economist), and I have been watching some of the Harvard CS 50 videos online.

In these videos the concept of pointers is introduced, and I am having a little bit of trouble with them.

Specificallyunderstanding the use of pointers, specifically, I am confused about the following line of code:

char* s = "my String";

I have two questions:

1. Is this code even legitimate? From some of the CS 50 videos, I would think that it is.

2. If the answer to (1) is yes, then I am quite confused. I thought the meaning of the statement: "char s" is that you want to create a new variable called s that is of type char*. This means that s is a pointer to some char variable (and at this point, we don't even know what this char variable is). If this is the case, then the right hand side of the above line of code should be an address in memory, and not a string.*

  1. Is this code even legitimate? From some of the CS 50 videos, I would think that it is.

  2. If the answer to (1) is yes, then I am quite confused. I thought the meaning of the statement: "char* s" is that you want to create a new variable called s that is of type char*. This means that s is a pointer to some char variable (and at this point, we don't even know what this char variable is). If this is the case, then the right hand side of the above line of code should be an address in memory, and not a string.

I would be more comfortable with something like: I would be more comfortable with something like:

char s[10] = "my string"; //10 because including extra space for null 0  
char* AddressOfs = &s;

but I don't think this is what is done in the CS 50 videos.

I don't have really any formal training as a computer scientist (I was trained as an economist), and I have been watching some of the Harvard CS 50 videos online.

In these videos the concept of pointers is introduced, and I am having a little bit of trouble with them.

Specifically, I am confused about the following line of code:

char* s = "my String";

I have two questions:

1. Is this code even legitimate? From some of the CS 50 videos, I would think that it is.

2. If the answer to (1) is yes, then I am quite confused. I thought the meaning of the statement: "char s" is that you want to create a new variable called s that is of type char*. This means that s is a pointer to some char variable (and at this point, we don't even know what this char variable is). If this is the case, then the right hand side of the above line of code should be an address in memory, and not a string.*

I would be more comfortable with something like:

char s[10] = "my string"; //10 because including extra space for null 0  
char* AddressOfs = &s;

but I don't think this is what is done in the CS 50 videos.

I am having a little bit of trouble understanding the use of pointers, specifically, I am confused about the following line of code:

char* s = "my String";

I have two questions:

  1. Is this code even legitimate? From some of the CS 50 videos, I would think that it is.

  2. If the answer to (1) is yes, then I am quite confused. I thought the meaning of the statement: "char* s" is that you want to create a new variable called s that is of type char*. This means that s is a pointer to some char variable (and at this point, we don't even know what this char variable is). If this is the case, then the right hand side of the above line of code should be an address in memory, and not a string.

I would be more comfortable with something like:

char s[10] = "my string"; //10 because including extra space for null 0  
char* AddressOfs = &s;
Source Link
Sam Y.
  • 111
  • 4
Loading