Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Poll

What programming topic would you want the next challenge to be about?  (It might be a good opportunity to focus on a subject you're not familiar with or to reinforce knowledge on one that you already know)

Control Flow
- 2 (2.2%)
Arrays, Strings, Pointers, and References
- 8 (9%)
Functions
- 4 (4.5%)
Basic object-oriented programming
- 30 (33.7%)
A bit more advanced OOP (Composition, Operator overloading, Inheritance, Virtual Functions)
- 18 (20.2%)
Templates
- 8 (9%)
Other (Explain)
- 4 (4.5%)
Working with files?  (Streams)
- 15 (16.9%)

Total Members Voted: 89


Pages: 1 ... 34 35 [36] 37 38 ... 78

Author Topic: Programming Challenges & Resources (#bay12prog) Initiative  (Read 96041 times)

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #525 on: December 07, 2010, 08:22:16 pm »

It's kind of... well, would trying to access char* with char[something] work? From this discussion I assume that the answer is no, but I kind of recall some book saying that it would.
Yeah, but it works for a different reason. As eerr said, it's even more confusing if you have different things that actually work the same (and the caveat is: almost always).
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #526 on: December 08, 2010, 09:19:56 am »

Uh. If so... I don't remember anymore in which cases you would use char* at all. It appears more transparent if to store a character array you use char[]. So, why would you want to store an array of anything using a pointer to the first member? The benefits of that I cannot fathom.
Logged

Shades

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #527 on: December 08, 2010, 09:26:05 am »

Uh. If so... I don't remember anymore in which cases you would use char* at all.

Pretty much any time you don't know the length of the array at initialisation.
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #528 on: December 08, 2010, 09:38:31 am »

You could also just need a pointer to a character somewhere ;)
Logged

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #529 on: December 08, 2010, 09:44:46 am »

Uh. If so... I don't remember anymore in which cases you would use char* at all.

Pretty much any time you don't know the length of the array at initialisation.
Don't you use dynamic lists for that? Argh. No. Is an pointer to an array comparable to a dynamic list?

You could also just need a pointer to a character somewhere ;)

Virex, please don't confuse him any more!

Why would that be? I mean if you need a single character, and then you want to pass a pointer to it, why create it with the pointer, not like a character?
Logged

Shades

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #530 on: December 08, 2010, 09:56:35 am »

Pretty much any time you don't know the length of the array at initialisation.
Don't you use dynamic lists for that? Argh. No. Is an pointer to an array comparable to a dynamic list?

Pretty much I'd either use a stl::string for a char* (or for a char[] for that matter) and some form of stl::vector for dynamic list. Of course I'd also use boost::array for an array too but that is besides the point.

Unfortunately there are still cases where you'd have to use c rather than c++ so you end up using the pointers or companies where 'dangerous third party data types', such as stl, aren't allowed and frankly it's easier to use char* than to right your own clone of decent well tested classes.

Why would that be? I mean if you need a single character, and then you want to pass a pointer to it, why create it with the pointer, not like a character?

The only case I can think of is when for some reason you need another function to set the value of a char but it can't be the return value for one of many reasons, in which case you could pass your local char as a pointer for the function to modify. I can't imagine you'd want to do it that often :)
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #531 on: December 08, 2010, 10:34:12 am »

if a function needs to set the value of a few chars, then you need to use char* for each
Logged

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #532 on: December 08, 2010, 10:43:39 am »

Yeah. It sets the value. But why would you initialise it through char*?
Logged

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #533 on: December 08, 2010, 05:16:15 pm »

You can reassign what a pointer points to, but not an array. Most of the time, string literals are not editable, so a pointer to a string literal cannor be altered as if it was an array. (char *c = "hi"; c[0] = 'n'; does not work, or if it does, you shouldn't rely on it, because many/all good compilers won't.)
A char[] is initialized with a copy of the string literal, as if you had used {'c', 'h', 'a', 'r', '\0'} rather than "char", so it is editable but not reassignable.

So, if you need to edit a string literal mid-function, use a char array. If you don't, but need it to point to different string literals, use a char pointer. If you need to do both, a pointer will work, but only if none of the edited strings are string literals, only char arrays created elsewhere. Or, in C++, simply use a string class rather than a char*/[], , and you don't have to worry about the difference. (Or something like char temp[] = "string"; char *p = temp;, but if you need to do that, then perhaps you should look for another way ot do it...)
Logged
Eh?
Eh!

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #534 on: December 08, 2010, 07:06:50 pm »

@Supermikhail:
No, nobody uses constructions like that. But you should be aware of how it works, it's rather fundamental.

Just keep in mind the difference between this:
Code: [Select]
const char a = 'a';
char b = a;
and this:
Code: [Select]
const char *a = "a";
char *b = a

The first will work, the second will not. In C++, strings and chars are not interchangeable. On the top, you're copying the actual value in a to b, which is why it works. On the bottom, you're copying the pointer, which points to different value types (a points to const char, b points to char), which is why it doesn't work.

Other than that, qwertyuiopas covered it well.
« Last Edit: December 08, 2010, 07:08:41 pm by Normandy »
Logged

eerr

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #535 on: December 09, 2010, 10:37:47 pm »

char[] s = "string";
char[] s ={'s','t','r','i','n','g','\0'};

Are identical.
Cstrings are character arrays with \0 at the end.

\0 is the line return character (Enter buton)
Logged

Shades

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #536 on: December 10, 2010, 04:11:42 am »

\0 is the line return character (Enter buton)

No, \0 (0x00) is a null byte.

Carriage return is \r (0x0D), new line is \n (0x0A) and the enter button is either one, the other or both depending on your OS.
However you are correct in that a null byte marks the end of a cstring.
« Last Edit: December 10, 2010, 04:15:01 am by Shades »
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd

nuker w

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #537 on: December 10, 2010, 04:14:08 am »

Posting to watch. I used to do a bit of C++ and then kinda left it. Might get into it again, now that it seems theres a topic here I can ask questions on.
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #538 on: December 10, 2010, 04:18:28 am »

I've been stuck for ages in my project now.

After a major rewrite of something totally unrelated, my OpenGL refuses to draw even the tiniest triangle. I can set the background-colour, that's it.

I know I must've screwed up somewhere, but there's so many places where this can go wrong. Bah, it takes all the fun out of programming.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Shades

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #539 on: December 10, 2010, 04:25:57 am »

I've been stuck for ages in my project now.

After a major rewrite of something totally unrelated, my OpenGL refuses to draw even the tiniest triangle. I can set the background-colour, that's it.

I know I must've screwed up somewhere, but there's so many places where this can go wrong. Bah, it takes all the fun out of programming.

Depending how much code it is I'd be happy to take a look at it if it would help find the error. I'm not bad with OpenGL stuffs. Also yes debugging is by far the most frustrating part of programming, which is why I unit test.
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd
Pages: 1 ... 34 35 [36] 37 38 ... 78