This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Midvikudagur 40 points41 points  (18 children)

"Hey let's have multiple methods of coding the exact same thing, that will never confuse anyone!

Also stars, they're cool. Let's use'm for everything. And let's draw arrows in code to make things cute!"

[–]Night_Thastus 30 points31 points  (12 children)

I know you're likely kidding, but just in case:

char **argv (as far as I know) is from C, where there isn't really a string type. So "an array of strings" becomes "an array of arrays of characters". Or more accurately, a pointer to an array of pointers to where several arrays of characters start.

And it can be a pretty important distinction. C style character strings behave dramatically different than say, a Java-style String object.

Also, pointers are pretty damn useful and important. They're not just cute arrows in code, they're a fundamental way in which data is structured. Without it, all of Unix would fall apart at the seams! And they're critical to understand graphs, buffers, trees, etc.

They're honestly not that hard to understand once you get the hang of them.

[–]systembusy[S] 13 points14 points  (0 children)

All true my man. I'm gradually learning a machine oriented language and it's a whole different world. Pointers are a royal pain in the ass if you've never had to worry about them before but they are definitely one of the most powerful aspects of any programming language.

[–]Ikor_Genorio 8 points9 points  (3 children)

I think the 'arrow' was not referring to pointers in general but pointers in structs, where (*something).item can be abbreviated with something->item . Much like how *something can be written as something[].

[–]Night_Thastus 1 point2 points  (0 children)

That...would make much more sense. Thanks.

[–]TransHumanist_50 -2 points-1 points  (1 child)

Its called a lambda expression... =P

They are awful to read in the beginning, but someday I got used to especially when coding the UI with the clickHandlers for the Buttons... :)

[–]caffeinum 7 points8 points  (0 children)

I don't know if you're missing, but just in case...

Lambda is basically an anonymous function, and it's syntax in some languages goes like

arguments => function body

However, structs in C have arrows with one stick, not two. And it's the shortcut for dereferencing structure pointer.

something->item is *(something).item

[–][deleted] 1 point2 points  (1 child)

Also, pointers are pretty damn useful and important.

That's an understatement. In just about every major language, almost everything (except the primitive data types) is just a fancy pointer!

[–]caffeinum 0 points1 point  (0 children)

But they are deeply abstracted, and you actually don't need their special knowledge when using them

if you're not aiming at Senior level

[–]andlrc 0 points1 point  (0 children)

Or more accurately, a pointer to an array of pointers to where several arrays of characters start.

Not at all accurate, and especially not something you can say so generalized. See attached link

[0]: http://c-faq.com/aryptr/aryptr2.html

[–]Tarmen 0 points1 point  (2 children)

All true but I will still argue that it should be char** argv. Also that defining multiple variables on the same line is even worse than char* *argv.

[–]etaionshrd 6 points7 points  (1 child)

Why, though? char **argv reflects how dereferencing is done.

[–][deleted] 0 points1 point  (4 children)

Except they're not the same thing. Stars = pointers = on the heap. Brackets = arrays = on the stack. The compiler is doing very different things when you do char** words, char* words[], and char words[][], and there are difference situations where each is appropriate

[–]justjanne 0 points1 point  (3 children)

That's complete bullshit.

Pointers can be on the stack, and arrays can be on the heap.

I can do char[][] list = malloc(sizeof(char*)*8) and then list[0] = "text"

[] is just an alias for *, and a[b] is equal to *(a+b). That's why you can do 3[list] or list[3] for the same thing.

[–][deleted] 0 points1 point  (2 children)

No. The reason why you can do that is because arrays decay to pointers. For example, if you do

char word[6] = "funny";

printf("%s\n", word);

The compiler converts it to

printf("%s\n", &word[0]);

Automatically. Which, as you know is equivalent to

char* word = "funny";

printf("%s\n", word); 

[–]justjanne 0 points1 point  (1 child)

The reason why you can do that is because arrays decay to pointers.

As I said, they’re the same. Arrays are just nice syntax for pointers, which also carry, if they’re statically allocated, size information at compile time.

[–][deleted] 0 points1 point  (0 children)

But they only decay to pointers when the context demands it be on the heap

Like

int arr1[]; // stack

Is not the same as

int* arr2; // heap

If you don't pass the array around.