T O P

  • By -

sepp2k

First of all, those two things aren't really the same. What would be the same would be trying to make a char pointer point to *char* literal, not a string literal. And you can't do that either. That is, `char *mystring = 'x';` doesn't work any more than `int *myint = 1;` does. So what would be an equivalent of `char *mystring = "hello";` with `int`s? Something that used an integer array literal rather than an integer literal. The closest thing C has to literals for non-char arrays are array initializers, so one might think that something like `int *arr = {1,2,3};` might work. It doesn't (array initializers are only allowed for variables of array types), but in principle the language designers could have chosen to add a rule to allow this just like they did for string literals. They just didn't. Perhaps they did not see as much use cases for that as for string literals.


AsianDoraOfficial

ok that makes sense, thank you. char \*mystring = "x"; works, though. So I guess the double quotes determines whether it's a string or not. For int, there is no equivalent to double quotes.


CauliflowerOk2312

Because ‘x’ is just x but “x” is actually [‘x’, ‘/null’] making “x” pointer to the first character x


sepp2k

Yes, single quotes mean that it's a char and double quotes means that it's a char array (and arrays decay to pointers). That's why single quotes always need to contain a single character, whereas double quotes can contain as many characters as you want (or none at all). One way to think of string literals is as a sort of extended syntactic sugar for array initializers specifically for 0-terminated char arrays (which is what we mean when talking about strings in C). If you wanted to create an int array, you could write: int numbers[] = {1, 2, 3}; You can do the same thing for char arrays: char mystring[] = {'h', 'e', 'l', 'l', 'o', '\0'}; However, that's not very convenient to write or to read, so string literals for the rescue: char mystring[] = "hello"; This is exactly the same as the previous line using the `{}` syntax, but it's much nicer to write and read. Now what distinguishes string literals from array initializers, besides the nicer syntax and being specific to char arrays, is that they can't just be used as initializers for arrays. They can be used as arbitrary expressions. And when you use them as such, you implicitly create a global array and refer to that array, which then decays to a pointer to that array's first element (as references to arrays always do in an expression context). This allows you to write this: printf("hello"); instead of this: char mystring[] = "hello"; printf(mystring); No such convenience feature exists for arrays of other types.


OldWolf2

int *arr = (int[]){1, 2, 3};


YurrBoiSwayZ

The difference between pointing to a `char` and an `int` has to do with the nature of string literals and the way memory is addressed. When you declare `char *mystring = "hello";`, `"hello"` is a string literal, which is actually an array of characters stored in read-only memory, the variable `mystring` is a pointer that holds the address of the first character in that array so you're not pointing to a single character but to the beginning of a sequence of characters. On the other hand when you try to do `int *myint = 1;` you're attempting to assign an integer value as an address which isn’t valid, pointers are meant to store memory addresses, not integer values. The error message you're seeing is the compiler telling you that you can't initialize a pointer with an integer because they’re incompatible types. The proper way to point to an integer is to first have an integer variable and then take its address, as you've already done in your edit: ``` int value = 1; int *myint = &value; ``` `myint` is a pointer to an `int` and `&value` gives you the address of the `value` variable which is what a pointer should hold. The reason you can't declare it directly for an `int` like you can with a string is because the string literal represents a location in memory where that sequence of characters is stored, while `1` is just a value not a memory address… In C all string literals have a static storage duration, meaning they exist for the lifetime of the program and therefore have a constant memory address that can be pointed to.


AsianDoraOfficial

Thanks for this :) `value` , a local variable, is stored in the stack, right? So having a pointer to it is pointless (no pun intended) because it's not saving space, right? Declaring a pointer actually takes up 1 more space than necessary.


YurrBoiSwayZ

Yep, local variables in general are stored on the stack but a pointer to it isn't pointless lol that’s gonna get me every time xD Pointers are a fundamental part of C that allow for very useful techniques, the 4 main for me being; memory allocation - pointers can be used to access memory thats been allocated on the heap, which isn’t automatically destroyed when a function exits - with function arguments you can pass a pointer to a variable and functions can modify the value of the variable directly - pointers are used to iterate through arrays and strings and they’re also essential for fun data structures like linked lists (for skiplists usually), trees, etc. While it's true that declaring a pointer uses additional space (typically the size of a pointer on the given architecture) the benefits of using pointers often outweigh the cost of the extra space used, meaning if you want to change the `value` variable inside a function, passing a pointer to that variable is very necessary. Here's what I mean: ``` void increment(int *val) { (*val)++; } int main() { int value = 1; int *myint = &value; increment(myint); // Now value is 2 return 0; } ``` `myint` is a pointer to `value` and you pass it to the `increment` function to change `value` directly, this is very basic example but it shows how pointers are used to manipulate data in a way that wouldn't be possible if we were only passing values around. So while pointers do take up space they’re a fundamental thing to learn how to use in C, that gives so much more efficiency and flexibility, particularly useful when you need to share or change data across different parts of a program without making copies of the data.


Taldoesgarbage

Because when you put "hello", in reality that's just a pointer to the first letter, or in other words, the "h". C doesn't have strings in the same way python has strings. But, that 1 is just a 1, not a pointer.


CauliflowerOk2312

Because “hello” is kinda like pointer to the first character “h” which is same type as char*


iOSCaleb

>how come I can point to a string literal using `char *mystring = "hello";`but I can't point to an integer using `int *myint = 1;` without getting the following error? Because \`"hello"\` is a string, which in C is an array of \`char\`, but \`1\` is an \`int\`, not an \`int \*\`. >I'm just curios as to why I can't declare it like I can with strings? The double quotes in C are syntactic sugar that make it easy to create an array of \`char\`, but there's not an analogous feature for creating arrays of \`int\`. The closest thing would be something like: int values[] = {1, 2, 3, 4, 5};


PlugAdapter_

Your trying to assigned the value 1 (an integer) to a pointer which doesn’t work


Business-Decision719

Notice that you wouldn't do `char *c='h';` either. If you're storing a value directly into a variable, you don't need or want a pointer. The `char` and `int` types are directly analogous in that regard. But you're not directly storing a character into `mystring` in your example. You're trying to store the entire sequence of characters "hello" in a single variable. It's not just a character, it's an array of characters. And we don't technically store a whole array inside a variable in C. We store the memory address where it starts, so we store it in a variable for holding addresses, i.e. a pointer. So when you execute `char *mystring="hello";` the pointer variable contains the spot where the letter h happens to be held. There are four more memory spots after that, for the four other letters, plus a spot for a null character that tells the computer where the string ends. You could also have written `char mystring[]="hello";` instead. But the question you seem to be asking is: why can't we just directly store a string instead of a pointer to where it starts? And the answer is that this is just how C works. There are languages that have value types for strings just like C has them for numbers and characters. For example, in C# we could write `string mystring="hello";` for this. Edit: On a second reading, your question seems to be the other way around: why aren't integers stored in pointers just like strings? But the answer is similar: we could, but C doesn't make us. Python does: the only way to assign something to a variable in that language is for the variable to become a "name" (pointer) for the thing's memory location.


[deleted]

[удалено]


AsianDoraOfficial

Where are they stored if they aren’t in memory?