Question regarding const arrays and passing them through functions

J

James

Hello,

First off, I know this code will not compile and am not asking for
someone to solve it for me.

What I am asking is from the code below, how does one first define an
array as a constant, and second pass the values in and out of
functions?

Here is the code; Thanks in advance:


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


/* Constant declarations */
const int MIN_VALUE=0;
const int MAX_VALUE=500000;
const int* A[ ];


/* Variable Declarations */
int N;
int n;
int k;
int d;


/* Function Prototypes */
int valueprint (const int MIN_VALUE, const int MAX_VALUE, const int A[
], int N, int n, int k, int d);
int adder (int n, int k, int d);
int progression (int n, int k, int d);
int maxsubsequencesum (const A[ ], int N, int n, int k, int d);


/* Function Declarations */
int valueprint (const int MIN_VALUE, const int MAX_VALUE, const int A[
], int N, int n, int k, int d)
{
printf ("Values: \n");
printf ("MIN_VALUE MAX_VALUE A, N n k d \n");
printf (MIN_VALUE," ",MAX_VALUE," ",&A," ",N," ",n," ",k,"
",d,"\n");
return 0;
}


int progression (int n, int k, int d)
{
/* sum the els sequence */
for (n = MIN_VALUE; n <= MAX_VALUE; n++)
{
for (d=MIN_VALUE; d <= MAX_VALUE; d++)
{
d=2*d;
return d;
}
if (n > MAX_VALUE)
n = ( n + ( ( k - 1 ) * d) );
else
n = (n + d);
return n;
}
return 0;
}


int adder (int n, int k, int d)
{
printf ("ELS Solver Program\n");
n=3;
k=7;
d=n+k;
return 0;
}


int maxsubsequencesum ( const int A[ ], int N, int n, int k, int d)
{
int ThisSum, MaxSum, i, j;
MaxSum = 0;
i = n;
j = k;
N = d;
for ( i = 0; i < N; i++ );
{
ThisSum = 0;
for ( j = i; j < N; j++ )
{
ThisSum += A[ j ];
if ( ThisSum > MaxSum )
MaxSum = ThisSum;
}
}
return MaxSum;
}


/****************************************************/
/****************************************************/
/********** **********/
/********** MAIN PROGRAM **********/
/********** **********/
/***************************************************/
/***************************************************/
int main (void)
{
valueprint (A[ ],N,n,k,d);
adder(n,k,d);
valueprint (A[ ],N,n,k,d);
maxsubsequencesum (A[ ], N, n, k, d);
valueprint (A[ ],N,n,k,d);
printf ("\nMaxSum: ",MaxSum);
printf ("\n");
valueprint (A[ ],N,n,k,d);
}
/*********************************************/
/*********************************************/
/*********************************************/
 
B

Bill Pursell

James said:
What I am asking is from the code below, how does one first define an
array as a constant, and second pass the values in and out of
functions?

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

void
print_array(const int *a)
{
while ( a[0] != -1)
printf("%d\t", *a++);
putchar('\n');
}

int
main(void)
{
const int array[] = {1,2,3,4,5,-1};

print_array(array);
return EXIT_SUCCESS;
}
 
B

Bill Pursell

James wrote:

/* Function Prototypes */

These are declarations...
int valueprint (const int MIN_VALUE, const int MAX_VALUE, const int A[
], int N, int n, int k, int d);
int adder (int n, int k, int d);
int progression (int n, int k, int d);
int maxsubsequencesum (const A[ ], int N, int n, int k, int d);


/* Function Declarations */

.... and these are definitions.
int valueprint (const int MIN_VALUE, const int MAX_VALUE, const int A[
], int N, int n, int k, int d)
{
printf ("Values: \n");
<snip>
 
B

Bill Medland

James said:
Hello,

First off, I know this code will not compile and am not asking for
someone to solve it for me.

What I am asking is from the code below, how does one first define an
array as a constant, and second pass the values in and out of
functions?

Presumably you mean that the contents of the array do not change.
Here is the code; Thanks in advance:


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


/* Constant declarations */
const int MIN_VALUE=0;
const int MAX_VALUE=500000;
const int* A[ ];

This declares A to be an array that contains pointers to constant integers;
I don't think that's what you mean.

const int *pA;

would declare pA to be a pointer that pointed to a constant integer (or the
first constant integer of an array of constant integers). Note that this
allows pA to be changed to point to some other constant integer.

(const int * const pA = &my_const_int; declares pA to be a constant pointer
to a constant integer; see how it needs to be initialised at the point of
declaration).

const int A[] = {1, 2, 3, 4};

would declare A to be a constant array of the four given integer values.

I suggest you try writing a few trivial 10 line programs to get the hang of
it, trying to change values etc.; many people get confused in this area.

You might find the "cdecl" program useful.
 
P

pete

Keith said:
Bill Pursell said:
James said:
/* Function Prototypes */

These are declarations...
int valueprint (const int MIN_VALUE, const int MAX_VALUE, const int A[
], int N, int n, int k, int d);
[...]

Yes. They're also prototypes.

James had labeled his prototypes as "prototypes",
which is good,
and his function definitions as "declarations",
which is correct, but a little strange.
 
J

James

Bill said:
James said:
What I am asking is from the code below, how does one first define an
array as a constant, and second pass the values in and out of
functions?

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

void
print_array(const int *a)
{
while ( a[0] != -1)
printf("%d\t", *a++);
putchar('\n');
}

int
main(void)
{
const int array[] = {1,2,3,4,5,-1};

print_array(array);
return EXIT_SUCCESS;
}
Bill,

Thank you very much, this explained quite a bit without a lot of
needless examples from the books I have..thanks again!
 
J

James

Bill said:
James said:
Hello,

First off, I know this code will not compile and am not asking for
someone to solve it for me.

What I am asking is from the code below, how does one first define an
array as a constant, and second pass the values in and out of
functions?

Presumably you mean that the contents of the array do not change.
Here is the code; Thanks in advance:


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


/* Constant declarations */
const int MIN_VALUE=0;
const int MAX_VALUE=500000;
const int* A[ ];

This declares A to be an array that contains pointers to constant integers;
I don't think that's what you mean.

const int *pA;

would declare pA to be a pointer that pointed to a constant integer (or the
first constant integer of an array of constant integers). Note that this
allows pA to be changed to point to some other constant integer.

(const int * const pA = &my_const_int; declares pA to be a constant pointer
to a constant integer; see how it needs to be initialised at the point of
declaration).

const int A[] = {1, 2, 3, 4};

would declare A to be a constant array of the four given integer values.

I suggest you try writing a few trivial 10 line programs to get the hang of
it, trying to change values etc.; many people get confused in this area.

You might find the "cdecl" program useful.
Bill,

Thank you very much for the explanations as well, they as well helped
quite a bit .. thanks again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top