Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

pointers in C

Name: Anonymous 2006-11-02 15:00

I have a little problem. way back in high school I took java, and never had to deal with these little things. now, at college, they're making me take C. I don't have anything against it, its just that.. well I'm not entirely sure what pointers are used for, or what they accomplish. would anyone care to elighten me?

Name: Anonymous 2006-11-02 15:06

A pointer is a variable which, instead of holding a value you work with, holds the position in memory where the value you actually want is. When-

Wait. Why don't you RTFM? There are many good C tutorials in the net, they're better than what I was going to say, just Google, it'll do ya good.

Name: Anonymous 2006-11-02 15:06

Name: Anonymous 2006-11-02 15:49

Make sure you learn the difference between * and &

Name: Anonymous 2006-11-02 17:44

void p(const string& s) { cout << s << endl; }
void p(const char *s) { cout << s << endl; }

int main() {
    string s = "lol noob";
    p(s);
    p(s.c_str());
    return 0; // you're the zero!
}

Name: Anonymous 2006-11-02 18:14

OP, by the time you finish learning C everything will make sense and you will feel silly for having made this post.  just wait.

also, I've never used Java, but doesn't it have pointers too?

Name: Anonymous 2006-11-02 18:17

>>6
Of course no--NULLPOINTEREXCEPTION

Name: Anonymous 2006-11-02 19:25

in before nurupo

Name: Anonymous 2006-11-03 2:52

Here's a good tutorial on pointers.

http://boredzo.org/pointers/

Name: Anonymous 2006-11-03 5:56

>>5
That's C++, not C. And you should return EXIT_SUCCESS rather than 0.
>>6
In Java, there are only pointers to structs, to put it shortly. I like it better than the C++ systems, but C is just fine.

A C pointer contains an address in your memory, just like an index in a array the size of your memory.

Name: Anonymous 2006-11-03 6:00

>>10
I forgot to mention http://cslibrary.stanford.edu/104/ =D
Magic wand of dereferencing ftw

Name: Anonymous 2006-11-03 7:31

>>10

As EXIT_SUCCESS is always defined as 0, it doesn't make any difference which one you use. Any decent programmer is going to know what "return 0" means from main().

Name: Anonymous 2006-11-03 11:11

Name: Anonymous 2006-11-03 11:16

#ifndef VMS
 #ifndef EXIT_FAILURE
  #define EXIT_FAILURE (1)
 #endif
 #ifndef EXIT_SUCCESS
  #define EXIT_SUCCESS (0)
 #endif
#else /* VMS */
 #ifndef  EXIT_FAILURE
  #define EXIT_FAILURE  0x10000002
 #endif
 #ifndef  EXIT_SUCCESS
  #define EXIT_SUCCESS  1
 #endif
#endif /* VMS */

Name: Anonymous 2006-11-03 12:22

Pointers in a nutshell:

- Pointers contain the memory location of a variable, rather than the value of the variable itself.
- Pointers have types (i.e., there are int pointers, float pointers, struct WHATEVER pointers, class WHATEVER pointers)
- There are even void pointers.
- Pointers can be assigned to each other, added to, and subtracted from.
- You can "deference" a pointer to get the value that it's pointing to.
- When you don't want a pointer to point to anything, set it to 0.
- Deferencing a 0 or null pointer generally has bad consequences.
- Array notation is just a different style of pointer notation, i.e., array[7] = *array + 7.  Pointer notation is faster.
- You can get a pointer to a function using pointer syntax.  It's a void pointer.
- Yes, you can even call functions via pointers, like *funcpointer(arg,arg,arg...)
- Yes, you can have an array of void pointers and use them to call functions
- Yes, you can have pointers to pointers and pointers to pointers to pointers.  Linked lists, do you use it motherfucker.

Name: Anonymous 2006-11-03 12:39

>>Array notation is just a different style of pointer notation, i.e., array[7] = *array + 7.  Pointer notation is faster.

1) You mean *(array + 7) ... *array + 7 will dereference *array and add 7 to that value.

2) Notation doesn't affect speed -- array[7] and *(array + 7) will compile into the same assembly.  Oh it may affect compilation speed, if this was like 20 years ago and we're working on shitty little steam-powered Tandy computers.

Name: Anonymous 2006-11-03 13:12

>>15
I wouldn't recommend pointer arithmetic to newbies.
Pointed functions are made of win.
Also, you can link your programs with efence to test them, it will make the program crash at the first seg fault (and not miles after leaving you with no clue on how it happened).

Name: Anonymous 2006-11-03 13:14

I get such a raging hardon for steam powered tandys

Name: Anonymous 2006-11-03 13:42

Here is an example of pointer:
include <stdio.h>

int main(int argc, char **argv)
{
    int array[] = { 1, 3, 5, 7, 9 };
    printf("%d\n", 2[array]);
    return 0;
}

Name: Anonymous 2006-11-03 14:32

>>19
I see what you did there

Name: Anonymous 2006-11-03 14:52

2[array]
People who make fun of C's array notation are monsters.

Name: Anonymous 2006-11-03 16:42

>>19
Die!  You do not belong in this world, you monster!!

Name: Anonymous 2006-11-04 11:38 (sage)

>>22
It was not by my hand that I am once again given cycles. I was called here by... _Kernighan_, who wished to pay me tribute.

Name: Anonymous 2009-01-14 15:14

GTFO

Name: Anonymous 2009-03-06 11:31


What kind of person actually cooks these   things up in   your computer right   now monitoring your   every move on   from writing things   in BASIC I   e you have   to understand that   the Japanese Government   resinded the law   protecting trade secrets.

Name: Sgt.Kabu폯쁓kiman╔具 2012-05-28 20:44

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List