Hi all,

I am in need of some assistance with addresses & pointers as my dysfunctional professor is of no help. (I'll be as succinct as possible.)

First, the program must call three subroutines from main to fill "n" array elements with a given value; for instance, one must set value[n] equal to 0, another sets value[n] to 1, and a third sets value[n] to -1. This I was easily able to do.

Next, a fourth subroutine must be written to return the number of positive, zero, and negative values in the array with the prototype "void signs(int value[25], int n, int *np, int *nz, int *nn);", where "n" is the number of array values, "np" is the number of positive values, "nz" zero values, and "nn" negative values. My question is, what would be the best way to modify my program such that np, nz and nn will be incremented given the conditions of my "signs" subroutine below?

Here is my rough cut of the current assignment, which builds and runs in National Instruments CVI but does not increment np, nz, and nn.

#include <stdio.h>
#include <stdlib.h>

void zero(int value[25], int n){
	value[n] = 0;
}

void one(int value[25], int n){
	value[n] = 1;
}

void n_one(int value[25], int n){
	value[n] = -1;
}

void signs(int value[25], int n, int *np, int *nz, int *nn){
		if(value[n] == 0)
			nz++;
		else if (value[n] == -1)
			nn++;
		else if (value[n] == 1)
			np++;
}	

int main(){
	int n, value[25];
	int np = 0, nz = 0, nn = 0;
	for(n = 0; n < 5; n++)
		zero(value, n);
	for(n = 5; n < 20; n++)
		one(value, n);
	for(n = 20; n < 25; n++)
		n_one(value, n);
	for(n = 0; n < 25; n++)
		signs(value, n, &np, &nz, &nn);
		printf("ZEROS: %d\nPOSITIVES: %d\nNEGATIVES: %d\n", nz, np, nn);       
	return 0;
}

I would greatly appreciate any help received for this.

Dani AI

Generated

A quick diagnosis and practical checklist.

The symptom (counters staying zero) comes from incrementing the pointer variable inside the subroutine instead of the integer it points to. As pointed out, the function must modify the value at the address you received, not move the address itself. Changing the routine so it updates the integer stored at the pointer fixes the counts — which confirmed.

Why this happens: when you pass an address (for example, the address of your positive-count variable) the parameter is an address. Incrementing that parameter advances the address and does not change your counter. Use the indirection (dereference) operator to read and write the integer at that address. Also watch operator precedence when combining unary operators and ++ so the increment applies to the integer, not to the pointer.

Checklist for debugging and hardening

  • Make sure the counters are initialized before you pass their addresses.
  • Pass the addresses of the counters to the function (so the function can write back results).
  • Inside the function, increment the integer stored at each pointer, not the pointer variable.
  • Prefer relational tests (value < 0, value == 0, value > 0) unless the data is strictly limited to -1/0/1.
  • Compile with warnings enabled (e.g., -Wall -Wextra) and run small prints or a debugger to verify each step.

Optional design ideas: return counts in a small struct or an output array to reduce pointer juggling, and use size types (size_t) for array lengths to avoid signed/unsigned mixups.

Recommended Answers

All 2 Replies

To increment (*ptr)++; Compile and run this little test program and you will see what it does.

#include <stdio.h>

void foo(int* n)
{
    (*n)++;
}

int main()
{
    int n = 0;
    int i;
    for(i = 0; i < 5; i++)
    {
        foo(&n);
        printf("%d\n", n);
    }
}

Ancient Dragon, you are quite the life saver. Thanks a million for jumping on this so fast; all I had to do was increment the pointers instead of actual variables.

-OTL

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.