Thread: Warning while passing array by reference to function

  1. #1
    Registered User
    Join Date
    Jun 2008
    Location
    USA
    Posts
    15

    Question Warning while passing array by reference to function

    Hello All,

    I'm a novice to C programming and I am working on a program that keeps giving me a warning that I can't seem to get rid of. But rather than posting the chunks of code from the original program, I managed to create a simplified example of what I was doing that still causes the same Warning. First, here's my code:

    Code:
    #include <stdio.h>
    
    int myFunc (char* inputArray);
    
    int main()
    {
    	char myArray[4];
    
    	myFunc(&myArray);
    	return(0);
    }
    
    int myFunc (char *inputArray)
    {
    	inputArray[3] = 'A';
    	return(0);
    }
    When attempting to compile this in Visual Studio (it is saved as a .c file) I get the following two warnings:

    1>.\Code.c(9) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'char (*)[4]'
    1>.\Code.c(9) : warning C4024: 'myFunc' : different types for formal and actual parameter 1

    I know the issue has something to do with passing the array by reference to the function but I'm not sure how to fix the issue. Any help would be greatly appreciated.

    God Bless,
    Jason O

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You don't need the ampersand (&), myArray is already a pointer.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would like to point out that it's better to say that you're passing the array via pointer, not by reference (that's a C++ term).
    Also note that when passing an array, you pass a pointer to its first element, so using the ampersand (&) on it, will make it a pointer to pointer, which isn't what you want.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147
    instead of

    Code:
    myFunc(&myArray);
    
    
    myFunc(myArray);

    would work


    myArray ==> myArray[0]

    so first element address will be passed in above case.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    87
    Pointers and arrays in C are *almost* the same thing. You can even use array index notation (the []'s) on a pointer! The array name is a varable containing (pointing to) the address of the first array element in memory. So, you don't need to use the & operator:
    Code:
    myFunc(myArray);
    You could also (but i'm not sure why you would want to) do this:
    Code:
    myFunc(&myArray[0]);
    The similarity between pointers and arrays should become obvious inside myFunc(). inputArray is declared as a pointer to a character, yet you are using array notation on it here:
    Code:
    inputArray[3] = 'A';
    If you wanted to do this in "pointer notation", I think the correct code would look like:
    Code:
    *(inputArray + 3) = 'A';
    But, either method works, and the former is probably easier to read.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Also note that when passing an array, you pass a pointer to its first element, so using the ampersand (&) on it, will make it a pointer to pointer, which isn't what you want.
    No, it's type would be pointer to array.

    As in
    int myFunc ( char (*inputArray)[4] );
    would be called with
    myFunc(&myArray);

    Having
    int myFunc ( char **inputArray );
    cannot be called with either
    myFunc(myArray);
    or
    myFunc(&myArray);

    Instead, you would need
    char *temp = myArray;
    myFunc( &myArray );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Salem View Post
    No, it's type would be pointer to array.
    Which I classified as a pointer-to-pointer, which is close to the truth, but not 100%.
    Although I haven't really tested it myself, so I'm not 100% sure of the behavior either...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    pointer to array != pointer to pointer.
    I strongly suggest you try it
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indeed... it's a pointer with set bounds.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Elysia View Post
    Indeed... it's a pointer with set bounds.
    Though nothing stops you from reading and writing data outside those bounds...
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indeed... it's like a suggestion more than set dimensions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jun 2008
    Location
    USA
    Posts
    15

    Smile

    Hi Everyone,

    Thank you very much for the fast replies! To solve my problem, I simply used Jason_m's suggestion of removing the ampersand from in front of the array in the function call and that fixed everything. Again thanks for all the great input!

    God Bless,
    Jason O

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Passing an array as reference to a function?
    By Kylecito in forum C++ Programming
    Replies: 10
    Last Post: 03-11-2006, 02:25 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM