Thread: functions which change arrays without pointers?

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    241

    functions which change arrays without pointers?

    the book I learn from gave a task to write a program which gets a matrix , and we have to write a function that switches 2 columns or rows the user inputs .
    this chapter still not covers the pointers subject . as far as I know a function can not change variables in the main function without using pointers .
    so , theoretically, can a function described here can be written without using pointers ? as far as I tried - it can not . am I missing something here or the book's just wrong?

    thank you!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Arrays are already passed to functions via a pointer (rather than copying the entire array) so there's nothing (extra) to be done here.

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    can you show a quick example or logic ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There's nothing extra, so no example is necessary as you should already know it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    ok, so I'm writing here 2 version of the function - one calling the matrix (matrix [4][4]) and the other don't - why doesn't it work?
    Code:
    int swich_col (int matrix,int col1,int col2)
    {
    int tmp[4];
    for (int i=0;i<4;i++)
     tmp[i]=matrix[i][col1];
    for (int i=0;i<4;i++)
     matrix[i][col1]=matrix[i][col2];
    for (int i=0;i<4;i++)
     matrix[i][col2]=tmp[i];    
    }
    the second one
    Code:
    int swich_col (int col1,int col2)
    {
    int tmp[4];
    for (int i=0;i<4;i++)
     tmp[i]=matrix[i][col1];
    for (int i=0;i<4;i++)
     matrix[i][col1]=matrix[i][col2];
    for (int i=0;i<4;i++)
     matrix[i][col2]=tmp[i];    
    }
    the error I get is in the compilation is :
    [Error] invalid conversion from 'int (*)[4]' to 'int' [-fpermissive]

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that in your first version, matrix is declared as an int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    what should I write instead ? every other try gives me different error..
    (btw , the matrix does only get integer as values, why is it an error?)

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would expect something like:
    Code:
    int switch_col(int matrix[4][4], int col1, int col2)
    Quote Originally Posted by Dave11
    the matrix does only get integer as values, why is it an error?
    You are trying to pass an array of arrays of ints as an argument when the corresponding parameter is an int. The matrix is not an integer, so of course it is an error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    it works! thank you very much!
    so in conclusion , in order to incorporate array/matrix in a function you have to call it as: <the type of vars inside the array> <the name of the array> <[a1][a2][a3][]...[][an]> (the number of dimensions of the array) ?

  10. #10
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by Dave11 View Post
    in order to incorporate array/matrix in a function you have to call it as: <the type of vars inside the array> <the name of the array> <[a1][a2][a3][]...[][an]> (the number of dimensions of the array) ?
    Not quite, in order to incorporate array/matrix in a function, you may define and/or declare it as: <the type of vars inside the array> <the name of the array> <[a1][a2][a3][]...[][an]> (the number of dimensions of the array). When you call it, you just pass the <the name of the array> as the corresponding parameter. There are also ways to define and/or declare a function which has a matrix as a parameter using pointer syntax.

    Do you understand the difference among define, declare, and call yet? You "define" a function when you write it. You "declare" a function when you supply a function prototype usually before the definition. The definition also serves as a declaration if definition comes before all uses of that function. You call a function when you use or execute it in another function.
    Last edited by pheininger; 01-17-2014 at 02:16 PM. Reason: minor grammar correction then added the commenct about pointer syntax.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays, functions pointers
    By zmaker5 in forum C Programming
    Replies: 6
    Last Post: 07-23-2007, 01:47 PM
  2. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 06:48 PM
  3. Functions+Arrays+pointers
    By Cdrwolfe in forum C++ Programming
    Replies: 21
    Last Post: 07-06-2006, 11:33 AM
  4. pointers, arrays, and functions oh my (newbie Q)
    By eazhar in forum C++ Programming
    Replies: 6
    Last Post: 07-21-2002, 06:26 PM
  5. Pointers, arrays , functions
    By sballew in forum C Programming
    Replies: 19
    Last Post: 09-16-2001, 11:12 PM