pointer as function argument

This is a discussion on pointer as function argument within the C Programming forums, part of the General Programming Boards category; Hi, its been a long time since I've done any C, and I've forgotten how to use a pointer in ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    110

    pointer as function argument

    Hi, its been a long time since I've done any C, and I've forgotten how to use a pointer in a function declaration. For example I want to declare a function with two arguments, an integer, and a pointer to an integer. The pointer will be the address of the first element of an integer array, and within the function I want to parse through the array using pointer arithmetic, but I can't remember the syntax, and everything I try won't compile.

    an example of the kinda thing I want to do:

    Code:
    void function(int *array_ptr, int x){
      for(i=0;i<x;i++){
        ans = (array_ptr+i) * 3.14;  //none of these compile
        ans = *(array_ptr+i) * 3.14;
        ans = &(array_ptr+i) * 3.14;
      }
    }
    PHI is one 'H' of alot more interesting than PI!

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    These are correct:
    ans = *(array_ptr+i) * 3.14;
    ans = array_ptr[i] * 3.14;
    However, if they don't work, you need to specify the compile errors.
    You should note that neither ans nor i are declared in the function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  2. C++ Function Pointer Problems
    By CodeBugs in forum C++ Programming
    Replies: 2
    Last Post: 07-28-2009, 08:06 PM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21