Thread: confused with pointers

  1. #1
    Registered User
    Join Date
    Apr 2007

    confused with pointers

    ok so I'm writing a program that will read a data file with 30 integers in it into an array, then pass the array to a function that counts the number of positive numbers, zero's, and negative numbers, then back in main print out those numbers. Its supposed to be done using pointers but Im confused as to what I am doing. heres my code so far

    #include <stdio.h>
    #define maxNum 30
    #define FILENAME "assign10in.txt"
    int readfile( int x[]);
    void signs( int x[], int nNum, int *nPos, int *nZero, int *nNeg);
    int main()
    	int nNum;
    	int x[maxNum];
    	int *nPos, *nZero, *nNeg;
    	nNum = readfile(x);
    	signs(x, nNum, nPos, nZero, nNeg);
    	printf("\n %d", nPos);
    	return 0;
    int readfile( int x[])
    	int nNum=0;
    	FILE *assign10in;
    	assign10in = fopen(FILENAME, "r");
    	if (assign10in == NULL)
    		printf("Error Opening Input File. \n");
    		while (nNum<maxNum)
    			fscanf(assign10in, "%d", &x[nNum]);
    			printf("%d ", x[nNum]);
    	return nNum;
    void signs( int x[], int nNum, int *nPos, int *nZero, int *nNeg)
    	int i;
    	for (i=0; i<nNum; i++)
    		if (x[i]>0)
    now what Im trying to write next is for if x[i]>0 then nPos will be increased by one, but with pointers im unsure of which way to do this

    &nPos++; ??

    I tried it several ways but when I throw in a print function to check it either prints 0 or a number such as 2289452 (I'm assuming thats the actual address?)

    anyone care to straighten me out?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Declaration of a pointer:

    int *p;
    Assignment of a pointer to a regular variable:

    int k;
    p = &k;
    Assigning a value to whatever a pointer points to:

    *p = 5;
    This means k is equal to 5. Whenever you do *p, it is resolved to be the variable that p points to.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    If you increment a pointer, you will increment the address it points to by the size of the object type in bytes. If you imagine memory as a long line of cubby holes, what's happening should be easier to visualize: you're moving to the next cubby hole. Obviously not what needs to be done.

    You seem to be confused about the difference between dereferecing and the address-of operator. The address of operator only fetches the address of an object: it can be useful if you want to store a different memory address in a pointer. It follows though, that the address-of operator is not intuitively on the left hand side of an assignment. The dereferencing operator fetches the value of the object at the address stored in the pointer. Because this is what we want to change, the dereference operator becomes important.

    We could do something like

    *nPos = *nPos + 1;

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. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  3. Hey guys..need help on pointers
    By Darkozuma in forum C++ Programming
    Replies: 5
    Last Post: 07-25-2008, 02:57 PM
  4. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  5. Replies: 4
    Last Post: 12-10-2006, 07:08 PM