Google Answers Logo
View Question
 
Q: Continuation of Question ID: 306889 Passing arrays-c ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Continuation of Question ID: 306889 Passing arrays-c
Category: Computers > Programming
Asked by: jimmyjrosu-ga
List Price: $5.00
Posted: 16 Feb 2004 04:52 PST
Expires: 17 Mar 2004 04:52 PST
Question ID: 307263
You may want to read the previous question and answer first in order
to understand this one..     How would the seconddimensional array be
passed to another fuction after it has been passed to a fuction (and
still be able to be modified)?
Answer  
Subject: Re: Continuation of Question ID: 306889 Passing arrays-c
Answered By: majortom-ga on 16 Feb 2004 06:36 PST
Rated:5 out of 5 stars
 
In the same way as before. You have the number of rows and the number
of columns; they were arguments to the first function. You also have
the pointer to the array data. Just pass them to a second function,
declared much like the first. Example programs, with and without using
gcc's extensions for
array passing:

* * * GCC VERSION * * *

#include <stdio.h>

void func1(int rows, int cols, int data[rows][cols]);
void func2(int rows, int cols, int data[rows][cols]);

int main(int argc, char *argv[])
{
  int data[10][20];
  func1(10, 20, data);
  /* Outputs 5 and 12, showing that both functions worked */
  printf("%d %d\n", data[5][6], data[2][3]);
}

void func1(int rows, int cols, int data[rows][cols])
{
  data[5][6] = 5; /* Do various things to data */
  func2(rows, cols, data);
}

void func2(int rows, int cols, int data[rows][cols])
{
  data[2][3] = 12; /* Do various other things to data */
}

* * * End * * *

An alternate program that does NOT use gcc's extensions to C:

#include <stdio.h>

/* This macro is a convenience; it works only if "cols" is set in the
  function in which it is used. The gcc extensions really do much the
  same thing, consider this a poor-man's version */
#define ROWCOL(array, row, col) (array)[(row) * (cols) + (col)]

void func1(int rows, int cols, int *data);
void func2(int rows, int cols, int *data);

int main(int argc, char *argv[])
{
  int data[10][20];
  /* Pass the address of the first element. Just passing 'data' would
    produce a compile-time error. */
  func1(10, 20, &data[0][0]);
  /* Outputs 5 and 12, showing that both functions worked */
  printf("%d %d\n", data[5][6], data[2][3]);
}

void func1(int rows, int cols, int *data)
{
  ROWCOL(data, 5, 6) = 5; /* Do various things to data */
  func2(rows, cols, data);
}

void func2(int rows, int cols, int *data)
{
  ROWCOL(data, 2, 3) = 12; /* Do various other things to data */
}

Request for Answer Clarification by jimmyjrosu-ga on 16 Feb 2004 07:34 PST
It may be easier just to post what i have so far(that i am testing): I
can get everything to change by following your excellent answer, but
well here just look:

#include <stdio.h>

#define WORD_SIZE 20
#define MAX_NUM_OF_WORDS 50

void firstchange(int, int, int *);
void secondchange(int, int, int *);

int main(void)
{
    words[MAX_NUM_OF_WORDS][WORD_SIZE];
    firestchange(MAX_NUM_OF_WORDS, WORD_SIZE, &words[0][0]);
    printf("\n%d%d", words[0][0], words[0][1]);
    return (0);
}
void firstchange(int maxnum, int maxword, int *wordss)
{
   wordss[maxnum*0+0]=4;
   wordss[maxnum*0+1]=9;
   printf("%d %d\n", wordss[maxnum*0+0], wordss[maxnum*0+1]);
   secondchange(maxnum, wordss, 0, 1);
   printf("%d %d\n", wordss[maxnum*0+0], wordss[maxnum*0+1]);
}
void secondchange(int max, int maxw, int wordss)
{
   

SHOOOOOOT!!!!!!!!!!!!-just answered my own question, in the original
program, I had a global variable named words, and then was wanting to
output the value of wordss, but was refering to words, no more closely
related variables for me.

Clarification of Answer by majortom-ga on 16 Feb 2004 08:15 PST
OK, sounds like your question is 100% resolved now, glad I was able to help.
jimmyjrosu-ga rated this answer:5 out of 5 stars

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy