[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

C / C++

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 13
Thread images: 2

File: q2.jpg (40KB, 482x446px) Image search: [Google]
q2.jpg
40KB, 482x446px
Can someone help me fix this c/c++ pointers code? I thought I understood pointers well but apparently not.

So first line obviously needs to changed to:

int val = 30;
int *p = &val;

then the second line should work, as it simply changes the address of p up 6? Dont see why that wouldn't work.

And for the third line, I am getting errors when trying to compile that. How to fix that line? (I can't move on since the rest requires what comes before it to compile).

Im aware the string in 4 will need to changed to char s[] = "....";


Sorry for the disorganized post, this is due in an hour and Im working on a different questions simultaneously.
>>
3: p = *p + 5;
>>
>>57224866

whatever you are trying to do there is an easier way
>>
>>57224866
> then the second line should work, as it simply changes the address of p up 6? Dont see why that wouldn't work.
For small values of "work".

Sure, changing the pointer won't result in an error, but the pointer now points at ... god knows what. So reading via the pointer will return some unspecified value and writing via the pointer will modify ... something, possibly resulting in a crash.

tl;dr: pointer arithmetic is only defined if the pointer points into an array.
>>
>>57224882
assigning an int to a pointer with a cast no?

>>57224906
to kill myself? gah

>>57224915
Ahhh, that makes sense. The assignment is vague on whether he wants us to correct it, or simply explain whats wrong. I'm leaning towards the latter now, since obviously there is no way to correct something like that.
>>
>>57224866
>And for the third line, I am getting errors when trying to compile that. How to fix that line? (I can't move on since the rest requires what comes before it to compile).
I don't think that should give a compile error, but it will likely crash when you try to run it (basically, p stated out pointing to the address of val, but in line 2 you set it to point to a different address, and your program doesn't have "rights" to that address, so the OS will likely kill your program when it tries to read/modify at that address in line 3.

>Im aware the string in 4 will need to changed to char s[] = "....";
Line 5 won't work though, since assigning to a C-style string (char array) doesn't work. Basically, C allows you to use the assignment operator when initially declaring an array (so line 4 will remain valid when you replace "string" with "char s[]"), however you can't assign to an array after that. This is because C/C++ treats arrays as pointers to the first element, and assigning a string literal to a pointer is nonsensical. So basically you have to access the individual character elements and replace them individually, remembering to add a null terminator after the last character (though there is a standard library function memcopy() to automate this for you).
>>
>>57224915
>tl;dr: pointer arithmetic is only defined if the pointer points into an array.
Isn't it also the case that if you increment a pointer to a struct by less than the struct's size that you'll be pointing to SOMETHING inside the struct?
>>
>>57224866

2nd line you try to add 6 to a memory address and get a segfault/undefined behavior

3rd line is the most fucked because you can't assign something that isn't stored

5th line you try to assign to something you didn't store again

on line 6 and 7 you don't get that an array without an index is a pointer to the start of the array.. and you try to assign to something you don't store

line 8 is an off by one error; an
int a[10];
starts at index 0 and ends at 9

and whatever you do on lines 9 10 and 11 is an abomination
>>
>>57225002

also it's defined if you malloc some memory

>>57224934

Why don't you just say what the point of this code is... Clearly you are going in a fucked up direction
>>
>>57225058
>also it's defined if you malloc some memory
That's basically an array on the heap, even if you don't use the [] operator. An array is just a contiguous block of memory, in the old days assembly programmers didn't even have the equivalent of [], they did everything with pointer arithmetic anyways.
>>
>>57224866
you'd have to be retarded to write code like that
>>
>>57225089
>>57225058
>>57225034
>>57225080

I didnt write this code, and ya, my prof is retarded, smart guy but doenst speak english.
Thanks for the help, i completed the quesiton,
just had to explain what was wrong with it.
>>
>>57224866
>Line 1
You initialize an integer pointer that points to the memory address 30 (0x1e).
>Line 2
You increment the pointer's address, so it now points to address 36 (0x24).
>Line 3
You increment the *value* that the pointer points to, which could be anything since you never initialized the value and have no idea whether that memory belongs to your program.
Thread posts: 13
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
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.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.