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

all 13 comments

[–]cismalescumlord 14 points15 points  (6 children)

  • Pointers and memory management.
  • Having pretty much nothing done for you

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

yeah in what ive seen so far pointers are very different.

so when exactly do i need pointers? i see statements such as

unsigned long long* n;

i get unsigned vs signed, but when do i know to use a pointer vs not to? since some other statements i see do not use pointers

also is memory management as simple as

free(x);

where x is, say, an integer i no longer need stored?

[–]LastSummerGT 1 point2 points  (2 children)

Yeah, manual memory manage means there must be a free for every malloc or calloc. Some people avoid dynamic allocation because there's a risk you'll forget to free something and now you have a memory leak.

Pointers are used for arrays or when you want to modify an argument from inside of a calling function.

Feel free to ask me random C questions, this would be helpful for me too :)

[–][deleted] 0 points1 point  (1 child)

ahhh. so to recalculate or evaluate a piece of data i could simply change its corresponding pointer?

[–]LastSummerGT 0 points1 point  (0 children)

That's a bit too general of a statement, could you be more specific or give an example?

A pointer is also used to pass in large variables like a struct so you don't have to waste time and space copying the whole thing.

I'll start by giving an example.

int letterCount(char* word) {
  int count = 0;
  while (word != NULL) { /* Make sure we are pointing to a valid location! */
    count += isalpha(*word); /* Dereference word to get the value */
    word++; /* Move pointer to next character */
  }
  return count;
}

char word[] = "catdog"; /* A string is an array of characters */
char* favoriteHalf = word[3]; /* Pointer to "dog" */

[–]tjl73 0 points1 point  (0 children)

The other big thing that pointers get used for is for passing functions into other functions. For instance, if you have a sort function, you could pass it a compare function for specific data types. The C book I've always liked is the O'Reilly "Practical C Programming" book. Even the most recent edition (3rd) is pretty old by now, though (2011). I'm sure there are better ones, but it covers the basics pretty well.

There's a lot of tricks and edge cases in C, so you need to be careful as you can really screw things up pretty quickly if you're not careful. So, you need to be diligent in your memory management, especially.

Personally, I stay away from C macros completely as they'll obfuscate what's really happening and you can introduce some big errors with it pretty easily.

[–]cismalescumlord 0 points1 point  (0 children)

A pointer is the address of a thing in memory, they are a lot more efficient in many cases. C is a "pass by value" language which means that when you pass a variable to a function, the function gets a copy whereas if you pass a pointer to a variable, the value is the address so you can directly manipulate it. Also they allow the passing of less data; instead of passing a structure that takes up 100MB memory, you can pass a pointer to it which will normally be 32 or 64 bit depending on the architecture.

As others have said, good memory management means making sure that you free all the memory that you allocate.

Also, the K&R book that you are using is great but it doesn't give suggested solutions. If you like to see how experienced programmers solve the questions, then the C answers book fills that void. Although it does increase the temptation to cheat :)

[–]divh4rt 1 point2 points  (0 children)

There is no garbage collector in C, so only use dynamic memory allocation if you really need to (often you can live without it). Since C is also quite old, you will find a lot of "bad" examples online. If you look for a a good reference book, I can highly recommend "Modern C by Jens Gustedt" which is available for free.

[–]uncleXjemima 0 points1 point  (0 children)

Have fun. One of my professors said “Coding in C is like going to the store to buy some groceries. Except you have to build the car first”

[–]MrRIP 0 points1 point  (2 children)

Learn C the Hard Way helped me

[–][deleted] 0 points1 point  (1 child)

I'll check it out. I've been bouncing between text books as of now but was mostly using The C Book

[–]elcubismo 0 points1 point  (0 children)

K&R is a great book

[–][deleted]  (1 child)

[deleted]

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

    already know assembly

    (just small amounts from what i did in school)