• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

Spoo

Member
I don't think I'll ever get my competitiveness completely out of sight, but maybe I could use a little more zenhabits in my life.

Thanks Spoo, really appreciate your 2 cents.

It's really just about being competitive in the ways that matter; be competitive with yourself. You can always be a better programmer, but if you look at specific people as benchmarks you're doing yourself a disservice. I find that people who question their own abilities at all times and work hard to overcome their problems often produce higher-quality work over those who think they know it all and are never seeking to be competitive with themselves. So keep your thoughts on your personal progression on you, specifically, and you'll find it works well.
 
huzzah, a new semester and I'm back in C and OpenGL, happy as a pig in shit. Self-taught both before but it's nice to go over all the stuff i missed or didn't understand properly the first time.

Quick question to you guys: at what age did you start coding?

I took a programming class when I was only 15 or 16 and it didn't click with me.

i started at about 6 or 7, did it professionally when i was 16-18, then worked a bunch of other jobs including dishwasher, farm hand and pastry chef before settling down into a career as a musician and completing an english lit. degree on the side. Now i'm back doing a masters in comp. sci at 34. It's the first time I've formally studied programming and I love it. Since i'm in the masters program there's a lot of other mature age students like myself but I don't feel particularly aged, even if the head tutors are usually younger than me.
 

Anony

Member
I don't understand how the resources work in VS.
I loaded some wav files into the resources folder (there seems to be 2 ways to do this? i dont know if I did it the correct way)
I have no clue on how to access the files. I would like to compile the exe with the wav so I don't have to reference it to access relative paths directory every time (because c# just makes it fucking difficult do do relative parts)
 
Introduction to Algorithms - This was the required book in my Algorithms class. The closest thing to a bible we have in Computer Science. I'm not kidding. This book covers pretty much everything. It dives deep into the analysis of algorithms and shows detailed proofs. Undergrads hate it, but grad students and professors love it.

yeah, this is probably what you want

Agreed. The CLRS is pretty much the standard.

Yep. Fall under the undergrad title so I "hate" it, but it's a pretty good book.

Just wish they indexed things starting at 0 instead of one, but I'm just picky about stupid stuff like that.

We use it in my Data Structures class (intro to algorithms)
 
There's still no consensus on 0 vs 1 as default base for arrays, so I would argue that it is a completely legitimate decision to make. 0 makes sense when you think of the index as an offset to an address. 1 makes sense when you think of it as an actual index.
 

Misguided

Banned
What do you hate about it?

I just sort of dislike object-oriented paradigms; the point is to make things simpler and easier for the programmer, so why is it so complicated? (overloading, autoboxing, wrapper classes, recursion, etc.)

Maybe I'm just dumb but I feel much more comfortable in a language like C++.

(edit: Yes, I realize C++ is an object-oriented language, but the need to adopt an object-oriented paradigm is much more "optional", since object-oriented functionality was kind of just thrown on top of regular C; you can use it a lot more like regular C with some extra functionality added to C++ and it's fine.)
 
So much stuff deprecated and forced try catch blocks.
Or i did something wrong.

I just sort of dislike object-oriented paradigms; the point is to make things simpler and easier for the programmer, so why is it so complicated? (overloading, autoboxing, wrapper classes, recursion, etc.)

The thing you will learn is that there are very different tools for very different jobs. There is no such thing as a bad language, just inappropriate ones at different times.

The very things that you don't like in Java are actually amazingly good for situations like large commercial projects with complex development cycles. When it is just you? Yeah you can code quite differently and cut a lot of corners in C or C++. When working in a team of 10 people (8 of which have no clue what they are doing) you'll be pretty happy those more forced design choices are there.
 

iapetus

Scary Euro Man
So much stuff deprecated and forced try catch blocks.
Or i did something wrong.

So you think it's better to write code that's guaranteed to bomb out completely in an error condition without letting the developer know that's a bad idea? Rather than letting your IDE throw in a try/catch block for you and handling the error condition sensibly?
 
Does anyone have ADT installed? I'm relearning Android.

Something REALLY bugs me. If you create a project using the wizard, set to 4.1.1, use dropdown action bar option. There's this bit of code from the template:
Code:
new ArrayAdapter<String>(
                        actionBar.getThemedContext(),
                        android.R.layout.simple_list_item_1,
                        android.R.id.text1,
                        names),
                this);

I can't find any "simple_list_item_1" under the layout folder, nor even as part of the code. Same goes for "text1". Where are they? What are they?
 
I just sort of dislike object-oriented paradigms; the point is to make things simpler and easier for the programmer, so why is it so complicated? (overloading, autoboxing, wrapper classes, recursion, etc.)

I'm not saying that Java is the best language ever or anything, but the reasons you state (with the exception of autoboxing, I'll give you that) don't make any lick of sense. The last one especially is pretty hilarious.

So basically, the reasons you don't like Java is because you don't like OO, and for some reason you don't like recursion.
 
Does anyone have ADT installed? I'm relearning Android.

Something REALLY bugs me. If you create a project using the wizard, set to 4.1.1, use dropdown action bar option. There's this bit of code from the template:
Code:
new ArrayAdapter<String>(
                        actionBar.getThemedContext(),
                        android.R.layout.simple_list_item_1,
                        android.R.id.text1,
                        names),
                this);

I can't find any "simple_list_item_1" under the layout folder, nor even as part of the code. Same goes for "text1". Where are they? What are they?

It's a build in XML layout document,

http://stackoverflow.com/questions/3663745/what-is-android-r-layout-simple-list-item-1
 

Milchjon

Member
I need some urgent beginner help for a test:

If I want a function to return a pointer in C, how do I do that? Do I need a * in front of the function's name?

For instance:
Code:
int *function(char string[]){
/*do something with the string*/
return string;
}

Is that the right way to do it?

Pointers are fucking with my head. I never know where to set the '*' and where I dont.

Like if I were to call that function above, would i need function(string); or function(*string); (Also, string[] would be equivalent to *string, right?).

Thanks in advance.
 

injurai

Banned
Yep. Fall under the undergrad title so I "hate" it, but it's a pretty good book.

Just wish they indexed things starting at 0 instead of one, but I'm just picky about stupid stuff like that.

We use it in my Data Structures class (intro to algorithms)

because mathematically it's easier, and it makes things really nice when representing trees as arrays or solving connected graph problems.
 
I need some urgent beginner help for a test:

If I want a function to return a pointer in C, how do I do that? Do I need a * in front of the function's name?

For instance:
Code:
int *function(char string[]){
/*do something with the string*/
return string;
}

Is that the right way to do it?

Pointers are fucking with my head. I never know where to set the '*' and where I dont.

Like if I were to call that function above, would i need function(string); or function(*string); (Also, string[] would be equivalent to *string, right?).

Thanks in advance.

In C, the * after the variable name indicates that the variable is a pointer, the * before indicates that you want to dereference that pointer and get the value out. The & gets you the address of a value, which you can store in a pointer.

Example, copying a string, takes a char pointer and returns a char pointer:

Code:
#include "stdlib.h"
#include "stdio.h"

char* copy_string(char* string, int len)
{
    int i = 0;
    char* new_string = malloc(sizeof(char) * len);
    for (i = 0; i < len; ++i)
    {
        new_string[i] = string[i];
    }
    return new_string;
}

int main()
{
    char* string = copy_string("abc\n", 4);
    printf("%s\n", string);
}


Example, 1 function returns a value, next function returns a pointer:

Code:
#include "stdlib.h"
#include "stdio.h"

int return_number_value()
{
    return 2;
}

int* return_number_pointer()
{
    int* value = malloc(sizeof(int));
    *value = 3;
    return value;
}

int main()
{
    printf("%i\n", return_number_value()); //value returned
    printf("%i\n", return_number_pointer()); //pointer returned
    printf("%i\n", *return_number_pointer()); //pointer dereferenced
}

Prints:

2 (The value from the first function)
146489352 (The address that the pointer returned by the second function is pointing to)
3 (The value in that address)

Star always comes after the name if you want to return a pointer from a function. Putting it in front makes no sense because you're no longer referring to a type then, you're referring to the value. You will never see *int, you will often see int*. Was a bit rushed writing this let me know if I left anything stupid out.
 

IceCold

Member
I just sort of dislike object-oriented paradigms; the point is to make things simpler and easier for the programmer, so why is it so complicated? (overloading, autoboxing, wrapper classes, recursion, etc.)

Maybe I'm just dumb but I feel much more comfortable in a language like C++.

(edit: Yes, I realize C++ is an object-oriented language, but the need to adopt an object-oriented paradigm is much more "optional", since object-oriented functionality was kind of just thrown on top of regular C; you can use it a lot more like regular C with some extra functionality added to C++ and it's fine.)

I was expecting you to mention how you prefer a language like Python or Lisp, but C++? Come on man. It's a cluster fuck of a language.
 

iapetus

Scary Euro Man
I'm not saying that Java is the best language ever or anything, but the reasons you state (with the exception of autoboxing, I'll give you that) don't make any lick of sense. The last one especially is pretty hilarious.

Well quite. There's nothing particularly tying Java to recursion. If you hate recursion, avoid LISP, sure. But not Java.

Actually, on second thoughts, if you hate recursion, re-educate yourself, then love both LISP and Java.

Sounds to me like a case of hating Java because it's the language in which he was taught, with the result that he's had to learn hard concepts in it, probably taught in a terrible way (most Java courses I've seen from universities and even large training companies have been pretty awful...)
 
Quick question to you guys: at what age did you start coding?

I took a programming class when I was only 15 or 16 and it didn't click with me.

Now I'm nearly 28 and I took another crack at it last year and it's coming along great. I feel like another few months of hard work and I'll be able to start coding professionally. The only thing is that I feel I've wasted so many years NOT learning this stuff, when it turns out that all I needed was the right teacher to make me love learning it all. I'm excited at the prospects of doing this professionally, but I feel like a (relative) dinosaur to my peers. Anyone else start coding this late in life and find success with it?

My first exposure to programming was in high school when I was 17. I picked VB during an internship in my first year of college. Learned C++ when I was 19. Learning a language and programming paradigm has never been hard for me. Learning the ecosystem, libraries and best practices for a particular language is a much harder endeavor.

I just sort of dislike object-oriented paradigms; the point is to make things simpler and easier for the programmer, so why is it so complicated? (overloading, autoboxing, wrapper classes, recursion, etc.)

Maybe I'm just dumb but I feel much more comfortable in a language like C++.

(edit: Yes, I realize C++ is an object-oriented language, but the need to adopt an object-oriented paradigm is much more "optional", since object-oriented functionality was kind of just thrown on top of regular C; you can use it a lot more like regular C with some extra functionality added to C++ and it's fine.)

lolwut. My guess is that you programmed in a procedural language for a long time. Try programming a generic linked list in C and one in a language like Python, and then tell me which is easier to understand, and maintain.

All languages have their strengths and weaknesses. For me it ultimately comes down to what type of problem I'm trying to solve. C for embedded systems; C# for windows development. Java for Android programming. If I'm trying to come with a solution to a problem without getting stuck in the syntax, I find myself using Python.

Bitbucket offers unlimited private repos

Bitbucket is what I use for my personal projects. Unlimited projects that can be accessed by up to 5 people for free. Github charges for all private projects.
 

Milchjon

Member
Alright, another simple question (in C):

Why is this returning syntax errors? It complains about the team name in the initialization (Syntax error before '.' token). I'm pretty sure I've done it the same way before without problems...

Code:
struct afce{
       char quarterback[20];
       char coach[20];
       int wins;
       struct AFCE *next;
       };
       
struct afce patriots;
patriots.quarterback="Tom Brady";
patriots.coach="Bill Belichick";
patriots.wins=12;
patriots.next=&dolphins;
 
There's still no consensus on 0 vs 1 as default base for arrays, so I would argue that it is a completely legitimate decision to make. 0 makes sense when you think of the index as an offset to an address. 1 makes sense when you think of it as an actual index.

For some reason, I was thinking about this the other day...

I honestly can't see any benefit to having 1 as the base, except that it is slightly easier for extreme newcomers to understand. 0 just works for everything, whether it's graphics (start drawing at i * spacing + offset, if i starts at 1, then that's just annoying) or just number systems in general (a 128 element array would only take one signed byte from 0 to 127).

I didn't know using 1 as a base was even a thing until my Java professor said that some prefer to start at 1 and to init the array as size+1, and all I could think of was why someone would do that. lol
 

Kalnos

Banned
For some reason, I was thinking about this the other day...

I honestly can't see any benefit to having 1 as the base, except that it is slightly easier for extreme newcomers to understand. 0 just works for everything, whether it's graphics (start drawing at i * spacing + offset, if i starts at 1, then that's just annoying) or just number systems in general (a 128 element array would only take one signed byte from 0 to 127).

I didn't know using 1 as a base was even a thing until my Java professor said that some prefer to start at 1 and to init the array as size+1, and all I could think of was why someone would do that. lol

No, I won't do what you want me to do.
 
Alright, another simple question (in C):

Why is this returning syntax errors? It complains about the team name in the initialization (Syntax error before '.' token). I'm pretty sure I've done it the same way before without problems...

Code:
struct afce{
       char quarterback[20];
       char coach[20];
       int wins;
       struct AFCE *next;
       };
       
struct afce patriots;
patriots.quarterback="Tom Brady";
patriots.coach="Bill Belichick";
patriots.wins=12;
patriots.next=&dolphins;

I haven't worked with C in a while, but I don't think you can do that. Both of you char arrays quarterback and coach can't be assigned the way you are doing. You might need to use string copy.
 

Milchjon

Member
I haven't worked with C in a while, but I don't think you can do that. Both of you char arrays quarterback and coach can't be assigned the way you are doing. You might need to use string copy.

Yeah, I just found that somewhere, and it makes absolutely no sense to me. Because if I do it like this

Code:
struct afce{
       char quarterback[20];
       char coach[20];
       int wins;
       struct AFCE *next;
       }patriots={"Tom Brady", "Bill Belichick", 12, &dolphins};

it works, and I don't get how the char array would behave differently if it's done the other way, especially since it works with other types.

Oh well, lesson learned, I guess. Thanks!
 
Yeah, I just found that somewhere, and it makes absolutely no sense to me. Because if I do it like this

Code:
struct afce{
       char quarterback[20];
       char coach[20];
       int wins;
       struct AFCE *next;
       }patriots={"Tom Brady", "Bill Belichick", 12, &dolphins};

it works, and I don't get how the char array would behave differently if it's done the other way, especially since it works with other types.

Oh well, lesson learned, I guess. Thanks!

A struct initializer does an implicit memcpy.
 

usea

Member
Must resist avatar quote.

Jebus man! 6?!!! Did you even know how to read by that time?
When I was 8 I made a really terrible rpg random battle game in GW-BASIC on my Tandy 1000. Basically you just kept encountering random enemies until you died. You could attack, magic attack, or heal. There was no way to gain back mp or hp.

Of course I didn't really know what I was doing. And although I continued to fiddle with programming on occasion, I didn't fully understand it until I went to college.

My 8 year old nephew can solve a rubik's cube pretty quickly. Kids are capable of a lot if they can sit in one spot for long enough.
 

arit

Member
Pointers are fucking with my head. I never know where to set the '*' and where I dont.

Like if I were to call that function above, would i need function(string); or function(*string); (Also, string[] would be equivalent to *string, right?).

Thanks in advance.

That's the problem starting with high level languages. Once you dip assembly, pointers become a not so complicated tool that can make things rather simple. But that's just another rant about CS Educations nowadays...
 
6. Though obviously I didn't do it professionally until quite a bit later - I must have been at least 12 before I made money with my code. :)

Heh, I started with qBasic when I was 6 too... I think my 386 still has those text adventure games! Next time I coded other than Basic was like 4 years ago though. Did some scripting etc. meanwhile but not much.
 

upandaway

Member
I'm starting to get an idea of how to not go crazy when planning code. Here are my findings.

Whenever you start to get an inkling of starting to get an inkling of feeling overwhelmed:
DO NOT take a 10 minute break
DO spend 10 minutes in front of a blank sheet of paper

Blank paper, man. It's magic.
 
I'm starting to get an idea of how to not go crazy when planning code. Here are my findings.

Whenever you start to get an inkling of starting to get an inkling of feeling overwhelmed:
DO NOT take a 10 minute break
DO spend 10 minutes in front of a blank sheet of paper

Blank paper, man. It's magic.

I think it's way easier to do a little planning in your head and start coding, then refactor that formless pulp of code into a beautiful program.

TDD is way better for this than a piece of paper since it actually forces you to think how you are going to use that code and then follow the cycle of make it fail, make it work, make it better. I'm not a full believer of TDD but it works very well in those situations.
 
So you think it's better to write code that's guaranteed to bomb out completely in an error condition without letting the developer know that's a bad idea? Rather than letting your IDE throw in a try/catch block for you and handling the error condition sensibly?

I have been mostly writing code for myself and school assignments.
For code in sold products or if i land a job i would use try catch blocks.

But for projects and assignments i will only want to see how to implement something on my own it kind of a hassle. Most of the assignments isn't even proper reviewed. They give a input and expect a output. They give the use cases and conditions and that is all i will do because most assignments are boring and not that huge.

I think it's way easier to do a little planning in your head and start coding, then refactor that formless pulp of code into a beautiful program.

TDD is way better for this than a piece of paper since it actually forces you to think how you are going to use that code and then follow the cycle of make it fail, make it work, make it better. I'm not a full believer of TDD but it works very well in those situations.

Is mostly my workflow when trying to learn a new api.
Just do something write the important piece in a notepad.
Then clean things up.
 
6. Though obviously I didn't do it professionally until quite a bit later - I must have been at least 12 before I made money with my code. :)

I was probably playing around at the same age. I remember trying all the graphics modes on my atari and poking away.

Problem was after about a year the "h" key broke. Was hard to code around that for ifs/thens but what could I do ;)
 
When I was 8 I made a really terrible rpg random battle game in GW-BASIC on my Tandy 1000. Basically you just kept encountering random enemies until you died. You could attack, magic attack, or heal. There was no way to gain back mp or hp.

ooh nice. i made multiple choice adventures and solar encyclopedia's on the C64 when i was that age. a few of my friends with kids have some kind of 8 year old computer loving child and I keep suggesting they should get them on to programming as soon as possible but it's kind of a shame those days are gone where your computer booted straight into a programming environment. Or even came with one as standard. the parents for the most are all a bit scared of computers themselves.
 
because mathematically it's easier, and it makes things really nice when representing trees as arrays or solving connected graph problems.

Oh I know. Just jarring for a bit to go from 0 -> n-1 to 1 -> n after using the former for so long.

That's the problem starting with high level languages. Once you dip assembly, pointers become a not so complicated tool that can make things rather simple. But that's just another rant about CS Educations nowadays...

Yeah. The class I took last semester that dealt with assembly made pointers and arrays (and subsequently c strings) that much easier to understand. It was also supposed to be taken after your entry level classes too.

edit: (C++) I have a list of elements which happen to be spaces in 2d space. I need to store and access them to manipulate them (delete, move, reshape/size etc.) Throwing ideas here, would it be best to declare a vector of strings and use it that way? Ideally each string element would have the coordinates of the object. Unless I can do a vector of float arrays?
 
Oh I know. Just jarring for a bit to go from 0 -> n-1 to 1 -> n after using the former for so long.



Yeah. The class I took last semester that dealt with assembly made pointers and arrays (and subsequently c strings) that much easier to understand. It was also supposed to be taken after your entry level classes too.

edit: (C++) I have a list of elements which happen to be spaces in 2d space. I need to store and access them to manipulate them (delete, move, reshape/size etc.) Throwing ideas here, would it be best to declare a vector of strings and use it that way? Ideally each string element would have the coordinates of the object. Unless I can do a vector of float arrays?

Can you make a class for a point or a struct?

Code:
class Point
{
    private int x;
    private int y;

   ...
}
 
I could but I need a dynamic container to hold the values. I figure <vector> would be easier (and it's either that or lists as a requirement so...)

Yeah, but what I was saying is that you could have a vector of points.

edit: unless I completely misunderstood your question.
 
Yeah, but what I was saying is that you could have a vector of points.

edit: unless I completely misunderstood your question.

Ah misread what you said.

And yeah I already have them in a class, but I figure strings would be better because it can either be a point, line segment, triangle, rectangle, or circle, hence why I was thinking an array of some sort would be best, ala strings.

Can anyone explain to me what the fuck is going on...

http://i.imgur.com/VU0YC8L.png

xcode is barfing all over the fucking place with this class definition. And I for the life of me cannot figure out why.

edit: had a weird reference that was making xcode go batshit, I think?
 

Harpuia

Member
I'm lost as to why compare isn't returning a 0 if the string entered is the same when reversed, can anyone point out where I'm going wrong?

Code:
#include <iostream>
#include <string>
using namespace std;

void reverse(string, int, string&);

int main()
{
	string input1, input2, returned;
	int length, palindromechecker;

	getline(cin, input1);

	length = input1.length();

	reverse(input1, length, returned);

	cout << returned << endl;

	palindromechecker = input1.compare(returned);

	cout << palindromechecker << endl;

	return 0;

}

void reverse(string input1, int length, string& returned)
{
	for(int i = length; i > -1; i--)
	{
	  returned += input1[i];
	}


}
 

Apoc29

Member
Ah misread what you said.

And yeah I already have them in a class, but I figure strings would be better because it can either be a point, line segment, triangle, rectangle, or circle, hence why I was thinking an array of some sort would be best, ala strings.

Can anyone explain to me what the fuck is going on...

VU0YC8L.png


xcode is barfing all over the fucking place with this class definition. And I for the life of me cannot figure out why.

Put header guards in your file:

Code:
#ifndef DRAWOBJECT_H
#define DRAWOBJECT_H

class DrawObject {
...
};

#endif

If your header file is included across multiple files you can get these errors.
 
Top Bottom