Thread: variable addresses and pointers

  1. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >cause the pointers just points to a int/char/float/etc..
    It's not an array of pointers because you don't define it to be. Notice the difference:
    Code:
    int  array[3]; /* Array of 3 int */
    int *array[3]; /* Array of 3 pointers to int */
    A pointer is declared with the asterisk. If it's not present, the variable is not a pointer.
    My best code is written with the delete key.

  2. #17
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    Oooh i see simple enough

  3. #18
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Pointers can also point to functions, not just variables.
    Are you trying to overwhelm him?
    My best code is written with the delete key.

  4. #19
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    About my coding style.. it comes from html.. wrote some sites and i think i have a clear way of seperating tables/functions, which i think is most important, sence everything is written in blocks and a clean line appears it easy to distinguish them
    And when i define variables i like to define them and assign a value to them on the same line if possible.

  5. #20
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    So i can pass the "return" value of a function to another function with a function pointer?

  6. #21
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >About my coding style.. it comes from html..
    Some of the worst styles I've seen follow guidelines from another language. Just keep in mind that your style is not widely acceptable C. I'm in a particularly good mood today, so I've been tolerating it. On another day, don't be surprised if I tell you to reformat your code if you want my help. Most of the time, I'll refuse to decipher poorly (or oddly) formatted code. I'm not unique in that feeling either.

    >So i can pass the "return" value of a function to another function with a function pointer?
    No, you can pass a function to another function and call it with a function pointer. That's a common use. Another is having a table of function pointers so that the actual function called can be determined at run-time rather than compile-time. But that's a more advanced use of pointers, and I would strongly recommend that you work on your fundamentals first.
    My best code is written with the delete key.

  7. #22
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Now I'm on topic.

    > About my coding style.. it comes from html..
    Ah, well HTML is a completely different world. If you want your instructors to be happy and others to bless your name you should learn a consistant, legible coding style. You don't use enough indentation and whitespace, which is people's biggest grump.

    [edit]
    Reading this carefully and slowly would give you some really good ideas.
    http://www.chris-lott.org/resources/...ill-annot.html

    > And when i define variables i like to define them and assign a value to them on the same line if possible.
    Unlearn that habit. Unless variables are intimately related, don't declare them on the same line. In fact, I'd recommend you'd stick to using one line per declaration until you get more familiar with C's type system.
    Last edited by whiteflags; 07-26-2006 at 11:43 AM.

  8. #23
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by tzpb8
    About my coding style.. it comes from html.. wrote some sites and i think i have a clear way of seperating tables/functions, which i think is most important, sence everything is written in blocks and a clean line appears it easy to distinguish them
    And when i define variables i like to define them and assign a value to them on the same line if possible.

    As Prelude told you earlier, take a look at the coding style of some of the folks who've been around here for a while -- Salem and Dave_Sinkula come to mind, among several others.

    Why is coding style important? Imagine you're making a living doing this, and you have to pick up someone else's code and make an important change -- especially if a production environment is down and money is being lost.

    If I picked up code from another programmer that looked like what you've been putting out today, it'd drive me crazy -- and that's on top of being under pressure to get the problem fixed, correctly and quickly.

    It's a lot easier to get help around here if you're C code is readable by C programmers
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

  9. #24
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    And what memory address do assign to that function pointer?

  10. #25
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    I dont know what that is if you say declaration, no.
    Defining a pointer: a[] is the same as *a, and *a is a pointer, So is (* a) [] really an array?
    Last edited by tzpb8; 07-26-2006 at 12:05 PM.

  11. #26
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by tzpb8
    And what memory address do assign to that function pointer?
    You don't manually pick a memory address in normal circumstances. You just set it to the address of an existing function.
    If you understand what you're doing, you're not learning anything.

  12. #27
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    pointers hold memory addresses nothing more right? so a function pointer would be the same i reconned.

  13. #28
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by tzpb8
    pointers hold memory addresses nothing more right? so a function pointer would be the same i reconned.
    That's right. But it's usually pretty meaningless to have a function pointer point to some random memory location. You almost always want it to point to another function. The exception might be for trying to trick the OS into running some code that might not be allowed to normally run, but I don't really have any experience with that.

    The point-to-another-function limitation might seem useless at first (why not just call the function directly?) but you'll find out eventually that it's still very useful in a number of circumstances. Look at the qsort() function. It accepts a pointer to a function that handles the comparisons. The signal() function also accepts a pointer to a function to handle the signal. From personal experience, I've also used them in a plugins implementation for a MUD-type game I wrote. I've also used it in a command-parsing/-calling routine where input commands were matched with functions that handle the commands. Keep coding and you'll find a reason to use them I'm sure.
    Last edited by itsme86; 07-26-2006 at 12:21 PM.
    If you understand what you're doing, you're not learning anything.

  14. #29
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    This is how you define/declare a function pointer? e.g: int (* name) (parameters) ?

  15. #30
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    Thanks, i got alittle lost after reading "declaring" and also after reading about function pointers this much so i wanted to atleast understand how to define them
    Though "
    Code:
    int (* a) []; // a is a pointer to an array of type int.
    "
    Doesn't make much sence. IOW "a" is an int array pointer that hasn't been assigned a value?
    Isn't it:
    Code:
    int *a[n]; // where n is any positive number :)
    Oh wait that is a pointer array, sry still abit lost.
    Last edited by tzpb8; 07-26-2006 at 01:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. char pointers and their addresses.
    By ens_leader in forum C Programming
    Replies: 5
    Last Post: 12-31-2007, 01:48 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM
  5. Pointers and their Addresses
    By incognito in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2001, 07:16 PM