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 ... 33 34 [35] 36 37 ... 78

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

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #510 on: December 06, 2010, 06:57:28 pm »

How is adding strings together related to literal character arrays? Strings are supposed to be objects of the String class, aren't they?
Nope, std::string is a really complicated piece of machinery, designed to work with arrays of characters (which is what a string really is).
Coming from a language with that stringstuff already built-in can be jarring.
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))

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #511 on: December 06, 2010, 07:46:11 pm »

Adding together 2 strings would cause a third array to be made, after which the date from the 2 char arrays that are added together is copied to the new array, at least in the most naive implementation. The C++ implementation may, and I emphasize may, do some memory sharing behind the scenes to prevent making new arrays all the time. This is however a wild guess and by no means a reason not to limit adding strings together a much as possible in possible bottleneck locations.
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 #512 on: December 07, 2010, 12:51:12 am »

Actually, just tested, adding string literals results in an error, since it can't add a const char* to a const char*. With gcc it's the more vague "invalid operand to binaty +" (or something similar). I guess some people would define an error as an unexpected result, especially if their main experience is higher level languages.
Logged
Eh?
Eh!

eerr

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #513 on: December 07, 2010, 02:28:00 am »

Java treats strings as objects.

C treats strings as arrays of characters.
and so does C++.
and what happens when you perform most operations on an array?

It is treated as a pointer.
and pointer +  pointer is, in almost all cases, garbage.
What? I'm pretty sure I read in one book that char * and char [] are interchangeable, that char[] is still a pointer to the first member. Did they trick me again? :'(

In most contexts char [] and char * are treated identically.
The pointer arithmetic, the pointer resolution are both very similar.


Virex, please don't confuse him any more!
« Last Edit: December 07, 2010, 04:51:10 am by eerr »
Logged

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #514 on: December 07, 2010, 04:10:25 am »

Vector? ???

Eh, I guess I got a lot more than what I came for already. And profound understanding of literals... Eee, a little test, and you can't output a sum of two char arrays. That's all I needed to become truly convinced.
Logged

eerr

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #515 on: December 07, 2010, 04:51:24 am »

ehhhhhhh.
Logged

Ari Rahikkala

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #516 on: December 07, 2010, 05:13:03 am »

char * and char[] mean the same thing, more or less

but they aren't the same as "hello" or const char *

They're not really the same thing at all. Just check with sizeof. char[] is really easy to cast to char* though :)
Logged

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #517 on: December 07, 2010, 05:22:57 am »

Huh? You mean <static_cast>? I must be wrong.
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 #518 on: December 07, 2010, 11:10:39 am »

char[] will create an array and initialize it with the string literal, rather than simply point to the string literal.

Code: [Select]
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char s1[] = "abcde";
    char *s2 = "abcde";
    printf("%s\n", s1);
    printf("%s\n", s2);
    system("pause");
    s1[2] = 'q';
    printf("%s\n", s1);
    system("pause");
    s2[2] = 'q';
    printf("%s\n", s2);
}

It will crash when trying to modify s2, but it can modify s1 without any problems. In both C and C++.
Logged
Eh?
Eh!

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #519 on: December 07, 2010, 02:09:25 pm »

In C with all compilers? I mean would any compilers ignore that char[] and char* are not the same?
Logged

eerr

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #520 on: December 07, 2010, 03:18:14 pm »

In C with all compilers? I mean would any compilers ignore that char[] and char* are not the same?
????
They are not treated identically.
Where would you get that crazy idea?

For starters, arrays have a constant size based on length*unitsize.
all pointers are the same size.
If compilers did some theoretically crazy shit like that, they wouldn't work at all.
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #521 on: December 07, 2010, 03:31:44 pm »

In C with all compilers? I mean would any compilers ignore that char[] and char* are not the same?
Char[] is an array with a defined size (at least, as soon as you assign anything to it) and therefor will be treated as an array. Char* is a pointer to a char. Compilers are already very generous in letting you assign multiple addresses with a single call to a char*, but it's still a pointer and not an array (which is a data type with a size and a pointer to the first element)
Logged

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #522 on: December 07, 2010, 03:39:13 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.

In afterthough, I could try that out myself... Yeah, I guess these things bear tryng out. But it's so nice here chatting with you, guys. :)
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #523 on: December 07, 2010, 03:47:03 pm »

To be honest I don't really know much about C, so if I tell you something you should always try it out yourself :P
Logged

eerr

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #524 on: December 07, 2010, 04:14:34 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.

In afterthough, I could try that out myself... Yeah, I guess these things bear tryng out. But it's so nice here chatting with you, guys. :)

That syntax does work on both, which is one reason why so many books say they are the same.

of course,
Code: [Select]
char[] array=char[3];
char* c=array;

array[x];
c[x];
(array+x);
c+x;
All yield the same result.
« Last Edit: December 07, 2010, 05:32:22 pm by eerr »
Logged
Pages: 1 ... 33 34 [35] 36 37 ... 78