445,851 Members | 2,104 Online 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 #include #include #include /* 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
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 #include void print_array(const int *a) { while ( a != -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"); -- 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 #include #include #include /* 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" /* 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 San Diego Supercomputer Center <* We must do something. This is something. Therefore, we must do this. Nov 4 '06 #5

 P: n/a Keith Thompson wrote: > "Bill Pursell"

 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 #include void print_array(const int *a) { while ( a != -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 #include #include #include /* 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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7814992,"math_prob":0.83959377,"size":7797,"snap":"2020-10-2020-16","text_gpt3_token_len":2117,"char_repetition_ratio":0.19158219,"word_repetition_ratio":0.6629712,"special_character_ratio":0.3542388,"punctuation_ratio":0.21489622,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9705246,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-02-19T15:00:53Z\",\"WARC-Record-ID\":\"<urn:uuid:e43266cc-911a-487d-9015-474f4a99fdc7>\",\"Content-Length\":\"44105\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b9a9d880-e612-42f5-84c5-10bf7e55d845>\",\"WARC-Concurrent-To\":\"<urn:uuid:9b274853-f2fc-4dcc-91fd-c9eddb8781e9>\",\"WARC-IP-Address\":\"104.197.32.212\",\"WARC-Target-URI\":\"https://bytes.com/topic/c/answers/559097-question-regarding-const-arrays-passing-them-through-functions\",\"WARC-Payload-Digest\":\"sha1:DUSPCJHCRQLYGIY6IB2UB7SBWFPWIEBK\",\"WARC-Block-Digest\":\"sha1:LZB7LK2YGUHFNPHBZV7XH54GFO2MVUH6\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-10/CC-MAIN-2020-10_segments_1581875144150.61_warc_CC-MAIN-20200219122958-20200219152958-00311.warc.gz\"}"}