Re: Feedback on C code?

You're conflating several concepts here.  I'm going to split this across two posts, one covering pointers and one covering const, because const is a separate thing.  Unfortunately the answers don't line up with your questions, so do me a favor and ask any further questions you still have, and I'll do my best to tackle them.

When doing an array, I suggest:

int numbers[] = {1, 2, 3, 4, 5};
const size_t NUMBERS_LENGTH = sizeof(numbers)/sizeof(numbers[0]);

Now the thing is:

p[i]

is the same as *(p+i), that is add the integer i to p's address (moving it forward in memory by 5 ints or 5 floats or whatever else--this isn't bytes, it's the size of the pointed-to thing) and then take the value.  So even if a is a pointer, you can still do:

for(int i = 0; i < a_length; i++) {
    int item = a[i];
    // stuff with item.

Second: no, you don't always need to check pointers to find out if they're null.  Pointers never become null unless you set them to null, so as long as you know it's not null you're fine.  That could be that you just pointed it at something, but it could also be that the caller of the function just isn't allowed to pass null in, or whatever else.

When setting a pointer to a number, instead of the address of a variable, you're pointing that pointer at that specific byte in memory.  It is fine to do that, as long as you don't dereference the pointer, or have some other reason to believe the pointer is pointing at valid memory.  C has a concept of undefined behavior, which is to say that it's behavior that can do anything.  When you read a pointer pointing at address 5 for instance, you could crash, you could get garbage data, your compiler could go "this can't be valid memory, let's optimize this read out", you could be talking to a device instead of memory, etc.  yes, hardware devices get addresses in memory, though usually your OS prevents you accessing them.  The only time doing this for real is a good idea is if you're doing kernel-mode programming, OS work, or embedded programming--in that case there are hardware-provided magic addresses that do things when you read from or write to them.  Otherwise it'll range anywhere from inconsequential random data happens to the OS crashes you for an invalid pointer read, and what exactly happens will even change run to run of the program.  In other words: don't do that.

You say the program exited without any complaints.  Depending how you were running it, it probably actually exited with a complaint, but that was hidden by being in the terminal and you didn't examine the exit code.  It can't work though, no.

Now to circle back to the loops.  You can actually compare pointers.  p1 < p2 if p1 points before p2 in memory.  You can increment pointers: p1++, p2++, etc.  You can subscript, as I did above, even if it's a pointer.  But to maybe try to help you get this, here is a function which prints a string:

#include <stdio.h>

void printString(const char *s) {
    for(; *s != '\0'; s++)
        putchar(*s);
}

If you don't know yet, strings are segments of chars in memory, and the last character of a string is always 0, which we write as '\0' (note the apostrophes which are silent with some synths) because it's a character, not a number.

-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector

Reply via email to