Thread: Pointers & Arrays

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    97

    Pointers & Arrays

    Dear all I am new in C programming and at the moment I am studying array and pointers.

    This piece of code is from an exercises I was looking and declares a function findMax as a pointer passing two arguments int arr[] and int n. The number of data values n is used to specify the size of the array.. What I dont undertand is the int arr[n] point.

    Can someone explain me how this piece of code is working?

    Thanks in advance

    int *findMax(int arr[],int n);
    int main(){
    int n,i,*p;
    printf("Enter number of data values");
    scanf("%d",&n);
    int arr[n];

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That declares arr to be a variable length array of n int elements. The variable length array feature was introduced in the 1999 edition of the C standard, but later became optional, so it is possible that your compiler might not support 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

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    97
    Quote Originally Posted by laserlight View Post
    so it is possible that your compiler might not support it.
    This is the reason I was asking for explanation because while compiling it returns an error of "error C2133: 'arr': unknown size"

    In order to create a variable length array in C I should use the malloc function?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, that would be an option.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers to pointers and pointer arrays
    By Dave11 in forum C Programming
    Replies: 5
    Last Post: 02-24-2014, 10:38 PM
  2. Pointers to pointers with arrays
    By Nurumla in forum C Programming
    Replies: 3
    Last Post: 07-18-2011, 11:53 PM
  3. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  4. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  5. pointers to 2d arrays
    By Diamonds in forum C++ Programming
    Replies: 4
    Last Post: 10-31-2002, 06:58 AM

Tags for this Thread