[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / r / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Board
Settings Mobile Home
/g/ - Technology

Name
Options
Comment
Verification
4chan Pass users can bypass this verification. [Learn More] [Login]
File
  • Please read the Rules and FAQ before posting.
  • You may highlight syntax and preserve whitespace by using [code] tags.

08/21/20New boards added: /vrpg/, /vmg/, /vst/ and /vm/
05/04/17New trial board added: /bant/ - International/Random
10/04/16New board for 4chan Pass users: /vip/ - Very Important Posts
[Hide] [Show All]


[Advertise on 4chan]


Sepples will make excuses for this.
>>
>>107718524
what does he mean "what address do you land on"? where does the output of the expression go? is it reused or are we doing it independently?

a chink or jeet wrote this text
>>
>>107718543
It's a Microsoft interview question kek
>>
>>107718524
4,4?
>>
>>107718524
ez pz
assuming this is x86-64

first line: &x + 1
means
take the address of the pointer to x and increment it by sizeof(int *)
so you have int ** and you increment it by the size of the type it points to: sizeof(int *)
so its gonna be 8
and the second line : x + 1
emans
take the pointer to x and increment it by sizeof(int)
so you have int * and you increment it by the size of the type it points to: sizeof(int)

the answer is 8, 4
and op is a faggot filtered by pointers
>>
>>107718975

If this line is in a normal function then x, the pointer, is a variable on the stack. How do you know where the stack pointer is?
>>
>>107718975
>>107719018
The variable shown here is an array. There is no int* for you to get an int** from.
>>
>>107719018
its an array
the address of an array is the same of its first element
the difference in behaviour in picrel, bw s_str_a and s_str_b emerges from the same property- one "shadow dereferencing"
youre passing a pointer to putstr, but in one case its a pointer with a dereferencing, in another its an offset

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct s_str_a
{
size_t size;
char *text;
};

struct s_str_b
{
size_t size;
char text[];
};


int main(void)
{
char text[] = "this is a string";
struct s_str_a *str_a = malloc(sizeof(struct s_str_a));
struct s_str_b *str_b = malloc(sizeof(struct s_str_b) + sizeof(text));

str_a->text = text;
strcpy(str_b->text, text);

puts(str_a->text);
puts(str_b->text);


// emits address of the array
fprintf(stderr, "&(str_b->text) : %p\n", &(str_b->text));
// emits address of the first element of the array
fprintf(stderr, "str_b->text : %p\n", str_b->text);

free(str_a);
free(str_b);


}
>>
>>107719040
arrays "decay" into pointers from the point of view of c, on a semantic level
but not from the point of view of its internals, how the semantics are expressed in asm
idk if that makes actual sense, it does to me
>>
also yeah. c
we dont serve sepplebois and dogs in this joint
we kindly ask our clients to leave their pet animals outside
>>
>>107719074
When an array is used in an expression, it will "decay" into a pointer to the first element of the array, with a few exceptions. One of these exceptions is the unary & operator, allowing you to make a pointer to the array.

&x does not give you int**. If it did, what int* would it be pointing to? There is no such object.
>>
>>107719150
the address of the array. duh
you add a level of reference to it
what else is it gonna do than "this 'ere int* shall now become an int** by virtue of addressing it through reference"
thats literally what youre telling the lang to do
that arrays are quirky, yeah, thats another thing.
pointers are a reference. not an address. theyre an abstract object. they opacify the actual memory layout. its for portability purposes afaik
>>
>>107718975
heh, I always thought "x" and "&x" would evaluate to the same type. oh well.
t. been coding in c++ for over 20 years



[Advertise on 4chan]

Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.