473,382 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

2D array, function call question in C

9
Hi
I have a question when I call a function and creating a pointer to a 2D array in the main function. "Main" call a function to input data in a array witch is declared in the main function.
Witch works perfectly well, however I would like only to send the first row of my 2D array, or only the second row, only the third row etc. A.t.m. I send the entire array, and don´t know how to just send one row at a time to the function, of the array. In the input function i input data from the keyboard using fgets.


Would really appreciate an answer

Thanks
Aug 22 '07 #1
10 6034
RedSon
5,000 Expert 4TB
If you want to send one row it is best to copy that row into a new array and pass that.
Aug 22 '07 #2
ahoyer
14
Hi
I have a question when I call a function and creating a pointer to a 2D array in the main function. "Main" call a function to input data in a array witch is declared in the main function.
Witch works perfectly well, however I would like only to send the first row of my 2D array, or only the second row, only the third row etc. A.t.m. I send the entire array, and don´t know how to just send one row at a time to the function, of the array. In the input function i input data from the keyboard using fgets.


Would really appreciate an answer

Thanks
because the rows are laid out in memory in row major, its quite simple to send just the individual rows. you dont need to copy anything.

try this code:
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. /*funct takes a 1d array as an argument and prints out
  4. its values, it needs to know the number of columns of your
  5. original array though*/
  6. void funct(int* array, int size){
  7.     int i;
  8.     printf("inside funct\n");
  9.     for(i=0; i<size; i++){
  10.         printf("%d ", array[i]);
  11.     }
  12.     printf("\n");
  13.     return;
  14. }
  15.  
  16. int main(){
  17.     int marray[3][5];
  18.     int i, j, k;
  19.     /*filling "marray" with values*/
  20.     for(i=0, k=0; i<3; i++){
  21.         for(j=0; j<5; j++){
  22.             marray[i][j] = k;
  23.             k++;
  24.         }
  25.     }
  26.     /*printing out "marray" just to make sure its full*/
  27.     for(i=0; i<3; i++){
  28.         for(j=0; j<5; j++){
  29.             printf("%d ", marray[i][j]);
  30.         }
  31.         printf("\n");
  32.     }
  33.     printf("pass each row\n");
  34.     /*passing the individual rows to the "funct" function*/
  35.     for(i=0; i<3; i++){
  36.         funct(marray[i], 5);
  37.     }
  38.     return 0;
  39. }
  40.  
marray is the 2d array, the first for loop just fills marray with numbers, the second one prints it out just to show you, and the third loop passes the individual rows into a function called funct that prints them out.

hope that helps!

:)
Aug 22 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
however I would like only to send the first row of my 2D array, or only the second row, only the third row etc. A.t.m. I send the entire array, and don´t know how to just send one row at a time to the function, of the array.
In C and C++ you cannot pass an array to a function. Function arguiments can only be a type or a pointer to a type. An array is not a type. Therefore, an array is a pointer.

The rule is that the name of the array is the address of element 0. When you pass the array to a funciton, you pass only the address of element 0.

Consider this:
Expand|Select|Wrap|Line Numbers
  1. int arr[3][4];
  2.  
There arr is an array of 3 elements. Each element is an array of 4 elements. Usng the rule, arr is the address of element 0, so arr is the address of an array of 4 int. So, declare a pointer to an array of 4 int:

Expand|Select|Wrap|Line Numbers
  1. int (*ptr)[4];
  2.  
You can assign the address of any row to this pointer:

Expand|Select|Wrap|Line Numbers
  1. ptr = arr;
  2. ptr = &arr[1];
  3. //etc..
  4.  
For a function, the argument must be a pointer to an array of 4 int:

Expand|Select|Wrap|Line Numbers
  1. void MyFunction(int (*arg)[4], int numelements)
  2. {
  3.  
  4. }
  5.  
All you need to is pass the address of an array of 4 int and the number of arrays of 4 int there are.
Aug 22 '07 #4
primpa
9
Thanks allot for the reply´s, they did clear some questions out.

I did not express my self clear enough thou, when I call a function and send my 2D array to it as a pointer like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. int input_data(char *);   //function declaration
  3.  
  4.  
  5. int main(char myarray[2][3]) 
  6. {
  7. input_data(myarray[2]);  // function call in main
  8. }
  9.  
  10. int input_data(char *arr)  // function start here 
  11. {
  12. ... // fgets to read in some data
  13. }
  14.  
  15.  
thou, this is not the problem, i want the function "input_data" to only recive the second row in the array "myarray" so the pointer "*arr" points to row 2 and column 0. maybe i am doing something wrong, but if i call the function twice, once with the argument (myarray[0]) and once withe the argument (myarray[2]) the later will overwrite the first row of the array.
maybe this is not possible, then correct me. otherwise i would gladly like to know how to do it =)

thanks for reply´s
Aug 22 '07 #5
2wycked
14
Expand|Select|Wrap|Line Numbers
  1.  
  2. int input_data(char *);   //function declaration
  3.  
  4.  
  5. int main(char myarray[2][3]) 
  6. {
  7. input_data(myarray[2]);  // function call in main
  8. }
  9.  
  10. int input_data(char *arr)  // function start here 
  11. {
  12. ... // fgets to read in some data
  13. }
  14.  
  15.  
Well, the first thing is that myarray[2] is not defined. myarray only has rows 0 and 1, so who knows what you're actually sending the function. Other than that, I don't see a problem with the code.
Aug 22 '07 #6
gsi
51
hi,
Your main() should take in 2 arguments , the first one being the number of actual arguments passed and the second one being the actual arguments itself. Secondly main takes in an array of char pointers as the second argument. Although arrays are passed in as implicit pointers, the second parameter in the main prototype must be explicitly declared as an array of char pointers not as what as u have done. This may be because,

Consider 2 declaration's
1. char *a = "hai";
2. char b[] = "hai"

Here pointer a may be made to point anywhere after this declaration but b since being an aggregate object allocated statically cannot be made to point anywhere else. The same is analagous to your case of myarray[2][5], since the space delimited user inputs are allocated as cstyle strings after the user types in and their corresponding pointers are assigned individually to your second array argument.
I am not sure of this , pls correct me if I am wrong.

It may be like,

Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char **myarray){
  2. input_data(myarray[1]);  //  ---> myarray[0] will be ur executable name !!!
  3. }
  4.  
Either way is fine , char **myarray or char *myarray[].

Thanks,
gsi.
Aug 22 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
int input_data(char *); //function declaration
That function is expecting a pointer to a single char.

Did you read my Post #4??

You can't pass an array to a function. All you can pass is the address of element 0.
Aug 22 '07 #8
2wycked
14
That function is expecting a pointer to a single char.

Did you read my Post #4??

You can't pass an array to a function. All you can pass is the address of element 0.
For 2D arrays, such as myarray[2][3], a value of myarray[i] would be a pointer, not an int (or char, or whatever type). Just as if you declared myarray as char** myarray, a value of *myarray will be a pointer, but a value of *(*myarray) will be a char.
Aug 22 '07 #9
primeSo
35
Thanks allot for the reply´s, they did clear some questions out.

I did not express my self clear enough thou, when I call a function and send my 2D array to it as a pointer like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. int input_data(char *);   //function declaration
  3.  
  4.  
  5. int main(char myarray[2][3]) 
  6. {
  7. input_data(myarray[2]);  // function call in main
  8. }
  9.  
  10. int input_data(char *arr)  // function start here 
  11. {
  12. ... // fgets to read in some data
  13. }
  14.  
  15.  
thou, this is not the problem, i want the function "input_data" to only recive the second row in the array "myarray" so the pointer "*arr" points to row 2 and column 0. maybe i am doing something wrong, but if i call the function twice, once with the argument (myarray[0]) and once withe the argument (myarray[2]) the later will overwrite the first row of the array.
maybe this is not possible, then correct me. otherwise i would gladly like to know how to do it =)

thanks for reply´s
The paramaters in main() is wrong. Someone has mentioned above, you can check it out. BTW, i think your understanding of array is not right. You want your function input_data() to only receive second row of the array "myarray", then optionally, you can choose to pass the second row of the array in main by
Expand|Select|Wrap|Line Numbers
  1.   input_data(myarray[1]);
, this mean, " a copy of the address of the first element in the second row will be make and pass to function input_data()" . You can use a for loop to pass one row each time you call the function, this can loop through every row and process each row accordingly. Remember, in the context of 2D array, when you only invoke the first subscript , it imply a pointer to a certain row only. In your case, you only have 2 row in the array, how do you pass myarray[2]? This mean the 3rd row in the array, compile error though.

You call the function twice? That mean the first time you call the function, no matter what value *arr has would "die" when u exit the function. I don't really understand what do you mean by "overwrite", this depends on your program's task.
Aug 23 '07 #10
gsi
51
Hi ,
Sorry for the confusion, Since u only need in column-zero it may be like

Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char **myarray){  // char *myarray[3] is also fine.
  2. input_data(myarray[i][0]); 
  3. }
  4.  
loop in the variable ' i ' for the no. of rows desired, in your case it is 1 to 2 (Assuming that the user input is two space delimited c style strings and u dont need the executable name). Exceeding this results in undefined behaviour (c/c++ Compiler's doesn't check this and even the runtime environment does't do this for execution speedup) .

Thanks,
gsi.
Aug 23 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
5
by: Steve | last post by:
Can anyone tell me if I can have an array of functions that take a variable number of parameters? If it is possible I'd like to know how to declare the array and the functions as its elements. I am...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
29
by: Vasileios Zografos | last post by:
Hi everyone, I need to build a function to plug it in a program (that I didnt make or can change) that should be called something like this: float someFunction(float x) { ...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
3
by: almo | last post by:
hi guys, I have question about writing a function that will store some data from a file in an array, then when that function is call in main, I can use that array in the main() for calculation. Here...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
12
by: lorlarz | last post by:
In the code sample below, how are arguments a legitimate argument to Array.slice? Function.prototype.bind = function(){ var fn = this, args = Array.prototype.slice.call(arguments), object =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.