[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Programming advise
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 10
Thread images: 1
File: katie.png (30 KB, 758x541) Image search: [Google]
katie.png
30 KB, 758x541
Hey, i've just started studying programming, and trying to put together a list of all the different types of arrays and how to use them into a usable program. I ended up with this, but it comes up with chapter6.exe not working when i try to "run" the program, using Codeblocks 13.12
>>
put them into a usable program** oops
>>
>>51788352
What's Hero1 supposed to contain? Are you writing the address of a static character string into an 8-bit integer?
>>
>>51788409
Hero1 is supposed to contain "Batman", have I done something wrong?
>>
>>51788447
char Hero1 = "Batman";

This line defines a single-character (probably 8-bit) variable named Hero1, and a static piece of string data containing "Batman\0", handled as a pointer to where the static data ended up when the program was built.

Old C's lax typing rules may let you assign the (32/64 bit) data pointer into the 8-bit variable but nothing useful is likely to happen. When the Hero1 variable is passed into printf (I think it gets promoted into an "int" because of varargs) its value is interpreted by %s as a pointer but the 8-bit char is too small to contain any meaningful address, potentially making the printf statement read something it shouldn't.
>>
>>51788447
hero1 is declared as a single char not a char array, it can hold one letter
>>
>>51788569
Wow you're right haha, I assigned [10] to it and it runs perfectly, thank you. I must admit I don't understand much of what you're saying, but thank you for pointing out the problem. I will try to get my head around this :P
>>
>>51788614
You should probably read a bit about how C's strings and arrays are basically just a thin convenience layer on top of raw memory pointers.

Also, if you didn't already, turn on all compiler warnings. If your Code::Blocks setup is using GCC, the relevant compiler argument is -Wall . You may find a checkbox for it somewhere in the project or compiler settings. Most compilers can produce warnings about many silly type errors like this.
>>
>>51788673
Where would I find these compiler warnings? and I am using GCC
>>
>>51788673
oh nevermind I got it I think, thanks that's really helpful!
Thread replies: 10
Thread images: 1

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

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.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.