• C or C++?
    200 replies, posted
[QUOTE=Eudoxia;16461743]Quick Question: Is C++ compatible with C?[/QUOTE] Depends what you mean. If you mean "Can I use C code in C++", then yes, sorta.
i think he meant, can i use C++ in C, no.
I meant "Can I use C in C++". So, thanks. This means that I can use some of the code, but not the complex stuff (Memory Allocation)
[QUOTE=Eudoxia;16461743]Quick Question: Is C++ compatible with C?[/QUOTE] Partially. You can mix the two easily if you know what you're doing, but they aren't directly compatible. It's much easier to use C in a C++ app than it is to use C++ in a C app. If you write C-style code and compile it as C++, the C++ guys will give you a nice long lecture. [QUOTE=Eudoxia;16461743]I think I'll go for C++. At least the program won' t collapse if I forget to put an ampersand[/QUOTE] It [i]will[/i] still collapse if you forget an ampersand. C++ pointers are just like C pointers. You might as well get used to the concept, regardless of which language you choose, since both require it.
[QUOTE=ROBO_DONUT;16464161]Partially. You can mix the two easily if you know what you're doing, but they aren't directly compatible. It's much easier to use C in a C++ app than it is to use C++ in a C app. If you write C-style code and compile it as C++, the C++ guys will give you a nice long lecture. It [i]will[/i] still collapse if you forget an ampersand. C++ pointers are just like C pointers. You might as well get used to the concept, regardless of which language you choose, since both require it.[/QUOTE] Why would you write C-Style code and compile it as C++ anyways?
C++.
[QUOTE=HoliestCow;16464431]C++.[/QUOTE] great contribution to the thread without any explanation.
[QUOTE=efeX;16464391]Why would you write C-Style code and compile it as C++ anyways?[/QUOTE] It usually happens because people learn from shitty books which teach the C way of doing things, or they were previously C programmers and figure out that "it works, so why not?"
[QUOTE=gparent;16464643]It usually happens because people learn from shitty books which teach the C way of doing things, or they were previously C programmers and figure out that "it works, so why not?"[/QUOTE] I know, it was more of a rhetorical question. It also bugs the shit out of me when people do it.
[QUOTE]It will still collapse if you forget an ampersand. C++ pointers are just like C pointers. You might as well get used to the concept, regardless of which language you choose, since both require it.[/QUOTE] I still don't know how to write pointers :( But I still fuck everything up with the ampersand: Alex's Way: scanf("Enter your name: %s",name) The Right Way: scanf("Enter your name: %s",&name)
[QUOTE=Eudoxia;16465369]I still don't know how to write pointers :( But I still fuck everything up with the ampersand: Alex's Way: scanf("Enter your name: %s",name) The Right Way: scanf("Enter your name: %s",&name)[/QUOTE] It depends entirely on context. The latter would [I]usually[/I] be wrong, because C strings are declared as char[] or char *, which are both pointers. I'll switch the example to something that better illustrates the differences. [code] int number; scanf("%d", &number); [/code] Here, "number" is declared as an "int". Without the ampersand, the argument of "scanf" would be the [I]value[/I] of the variable "number". scanf() does not want some random value. scanf() needs the location to store its result at. We use the ampersand to get the [I]address of[/I] "number", so that scanf() can place the result in that variable. [code] int *number = malloc(sizeof(int)); scanf("%d", number); [/code] Here, "number" is the address of, or pointer to, an integer. Initially, it contains some random value (not random, really, just some garbage that was in memory), so we use malloc() to give it a valid buffer to point to. No ampersand is required because "number" is just an address.
Cool
Or, with C++, you can avoid these mistakes all together: [code] std::cout << "Enter your name: "; std::cin >> name; // MAGIC [/code]
[QUOTE=nullsquared;16465883]Or, with C++, you can avoid these mistakes all together: [code] std::cout << "Enter your name: "; std::cin >> name; // MAGIC [/code][/QUOTE] I know all about iostreams, but the point here is that you [I]need[/I] to learn pointers unless you never plan on writing anything more complex than std::cout << "Enter your name: " << std::flush. Every API, library, and framework I can think of requires a working knowledge of memory management and pointers.
[QUOTE=ROBO_DONUT;16466083]I know all about iostreams, but the point here is that you [I]need[/I] to learn pointers unless you never plan on writing anything more complex than std::cout << "Enter your name: " << std::flush. Every API, library, and framework I can think of requires a working knowledge of memory management and pointers.[/QUOTE] You must not know alot of API's, libraries, or frameworks.
[QUOTE=efeX;16467325]You must not know alot of API's, libraries, or frameworks.[/QUOTE] All C and C++ libraries require knowledge of memory subsystems and their handling. Hell, even garbage collected languages still require a large amount of knowledge of the underlying systems especially when writing code for performance. Ergo, you're absolutely retarded.
[QUOTE=blankthemuffin;16467730]All C and C++ libraries require knowledge of memory subsystems and their handling. Hell, even garbage collected languages still require a large amount of knowledge of the underlying systems especially when writing code for performance. Ergo, you're absolutely retarded.[/QUOTE] learn2read
[QUOTE=efeX;16468214]learn2read[/QUOTE] lern2meme
[QUOTE=efeX;16464391]Why would you write C-Style code and compile it as C++ anyways?[/QUOTE] Because the C++ compiler has better error checking.
[QUOTE=tarkio;16483140]Because the C++ compiler has better error checking.[/QUOTE] You missed the point.
So now I'm learning C++, and then i'll learn C. Quick (really f*cking n00bish) question: What can I do with pointers? They store the address of a byte or variable or whatever, and then what?
[QUOTE=Eudoxia;16485227]So now I'm learning C++, and then i'll learn C. Quick (really f*cking n00bish) question: What can I do with pointers? They store the address of a byte or variable or whatever, and then what?[/QUOTE] You can pass a pointer to a function and have the function modify the value the variable points to. You can only return one variable, but by using pointers you can modify several variables.
[QUOTE=PvtCupcakes;16485567]You can only return one variable, but by using pointers you can modify several variables.[/QUOTE] Correction, you can only return one [I]typed object.[/I] If you defined a struct with a whole bunch of fields and stuff in it, then you can return one of those with all the variables inside, but it is generally discouraged. We like return values to be completely contained within a single return register.
[QUOTE=tarkio;16483140]Because the C++ compiler has better error checking.[/QUOTE] Except C++ is not a strict superset of C
[QUOTE=blankthemuffin;16487426]Except C++ is not a strict superset of C[/QUOTE] Yeah, it's stricter in that some C code just won't work!
Ok, so I can change lots of stuff at the same time with pointers.
[QUOTE=Eudoxia;16487718]Ok, so I can change lots of stuff at the same time with pointers.[/QUOTE] Yeah but in many cases you're better off using references.
[QUOTE=gparent;16487700]Yeah, it's stricter in that some C code just won't work![/QUOTE] I was more pointing out that C has moved on since C++ bastardised it.
[QUOTE=blankthemuffin;16488158]I was more pointing out that C has moved on since C++ bastardised it.[/QUOTE] Yeah I totally read that like a moron. Mah bad.
[QUOTE=Eudoxia;16487718]Ok, so I can change lots of stuff at the same time with pointers.[/QUOTE] It's a lot more than that. Pointers are a fundamental building block of many data structures. You need them to implement trees, linked lists, and more. Trees, specifically, are essential to finding efficient solutions to many problems because their depth grows logarithmically with the number of elements they contain and they are very easy to traverse. Linked lists and whatnot are important in C, but I think C++ has a number of equivalent data structures built-in. You also need pointers if you want to call a function and have it modify an existing value. For instance, you have a struct called "playerstats", and you want to write a function to decrease the "hp" value. The following will not work: [code] void damage(playerstats player) { player.hp--; } [/code] This doesn't work because it creates a [I]duplicate[/I] of the structure on the stack. The original playerstats, which was passed to the function, remains unmodified. The following will work: [code] void damage(playerstats *player) { player->hp--; } [/code] The actual location of the player's stats are passed into the function, then the function goes out and modifies the original, such that the changes are persistent. Pointers are also important if you want to save memory (duplicating large structures many times will eat up RAM fast) or access specific regions of memory to directly control hardware (yes, I brought up more low-level crap; cry about it). Really, understanding pointers will just make your life a whole lot easier. You'll find that you can use pointers to find simple and elegant solutions to problems that would otherwise prove difficult. [editline]13:37AM[/editline] Also, I should mention that you can also do "void damage(playerstats &player) { player.hp--; }" in C++ before anyone freaks out about it. Using the ampersand in this context has an entirely different meaning than the usual "address of" operator. In this case, it's a shortcut that allows you to reference a value without treating it as a pointer. (Personally, these little syntactic inconsistencies irk me, but some find them convenient.)
Sorry, you need to Log In to post a reply to this thread.