By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

Question regarding const arrays and passing them through functions

P: n/a
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);
}
/*********************************************/
/*********************************************/
/*********************************************/

Nov 4 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a

James wrote:
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;
}

Nov 4 '06 #2

P: n/a

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>

--
Bill Pursell

Nov 4 '06 #3

P: n/a
James wrote:
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 Medland
Nov 4 '06 #4

P: n/a
"Bill Pursell" <bi**********@gmail.comwrites:
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);
[...]

Yes. They're also prototypes.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 4 '06 #5

P: n/a
Keith Thompson wrote:
>
"Bill Pursell" <bi**********@gmail.comwrites:
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);
[...]

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.

--
pete
Nov 5 '06 #6

P: n/a

Bill Pursell wrote:
James wrote:
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!

Nov 5 '06 #7

P: n/a

Bill Medland wrote:
James wrote:
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 Medland
Bill,

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

Nov 5 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.