help with pointers? c

Tags
c error 73 memory management pain in the ass pointers segfault 
Labels
Members allowed to view this conversation
Everyone

Need guides on learning how pointers work and when I should be using them… is the main reason why im not learning c.

here's what I got so far:

#include <stdio.h>

int main(void) {
char message [] = "The raids will never stop";
char * message_POINTER = message;

while(1) {
char characTer = *(message_POINTER++);
if(characTer == '\0') break;
else {
printf("%c", characTer);
}
}

printf("\n");
return 9;
}


idk how to read code


Pointers are pretty self-explanatory. "When to use pointers" should be whenever you can, in my opinion.


kirklandstoreproductsmin needs to add a codeblock feature that only supports PHP highlighter -syntax.

void * - go to this post

here's what I got so far:

#include <stdio.h>

int main(void) {
char message [] = "The raids will never stop";
char * message_POINTER = message;

while(1) {
char characTer = *(message_POINTER++);
if(characTer == '\0') break;
else {
printf("%c", characTer);
}
}

printf("\n");
return 9;
}

brother, there's an easier way to print text rather than character by character. please learn java first you absolute faggot

void * - go to this post

Need guides on learning how pointers work and when I should be using them… is the main reason why im not learning c.

* after a type : memory address to a variable of that type
* before a name and no type : dereferencer : gets the value at that address, casts it to the type unless you use void *, which it neeeeds to be manually casted.
& before a name : syntax to get the memory address of a variable (not needed for arrays since they decay)

you can increment or decrement an array's pointer to access the next or previous value since it points to the first element of the array. you willn't get a size from it.

you will use pointers so you don't inflate your program's memory to shit. you'd rather give someone your address (4 strawberry lane, media PA 19063) rather than an entire copy of your house, right? plus, there are some cases where you have to use pointers. i personally wouldn't use pointers if it came to valuable objects, like fiber pills.

can't make out the paragraphes? WATCH A FUCKING VIDEO TUTORIAL YOU NIGGERTWAT


This can be simplified to:

#include <stdio.h>

int main(void) {
  char message [] = "The raids will never stop";
  printf("%s\n", message);
  return 0;
}

I don't know why you're returning 9 (returning 0 is the standard thing to do), you really don't need to print a string character by character. Did you get this code from somewhere else or did you write it yourself? In any case the %s formatting tag will print any valid (null-terminated) string so there is no need for whatever it is that was going on before.

If you did want to loop through a string character by character, you would just index it like an array. E.g:

#include <stdio.h>
#include <string.h>

int main() {
  char message[] = "The raids will never stop";
  for (int i = 0; i < strlen(message); i++) {
    printf("%c", message[i]);
  }
  printf("\n");
  return 0;
}

Ultimately it isn't that complicated, and it's best to not make it complicated needlessly. Maybe a language like python would be more your speed, but C isn't difficult when you know what you're doing.

One last thing, you generally use pointers when you want to be able to dynamically allocate and de-allocate memory (variables). I.E. writing efficient programs that only use memory when it's needed and free it up immediately once they're done with it.


just use phppythonscript


Hey! You need to log in or create an account to do anything on this forum.

6,142 posts - 1,371 conversations - 1 member online

  • Display avatars