Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C problem

  • 12-03-2003 4:08pm
    #1
    Closed Accounts Posts: 2,256 ✭✭✭


    Have to program conway's life. My code is here and upon compiling i'm presented with the following errors
    gcc -c conway.c
    conway.c: In function `main':
    conway.c:66:warning passing arg 3 of `secondary' makes pointer from integer without a cast
    conway.c:66:warning passing arg 4 of `secondary' makes pointer from integer without a cast

    I have no clue as to what these errors mean or how to fix them.

    line 98 on hashjava.com corresponds to line 66 of the code.

    any help will be greatly appreciated


Comments

  • Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭hussey


    should this line :
    secondary (jarray, iarray, conways[SIZE_I][SIZE_J], conways2[SIZE_I][SIZE_J]);

    be :
    secondary (jarray, iarray, conways, conways2);

    as otherwise you would be sending an int as a int[][].

    as in conways[j] will evaluate to a int rather than an 2d array


  • Closed Accounts Posts: 14,483 ✭✭✭✭daveirl


    This post has been deleted.


  • Closed Accounts Posts: 285 ✭✭marauder


    Daveirl Did you test this code?
    there are some serious flaws in it...

    srand((unsigned int)time(NULL));

    also missing #include <time.h>

    your /* Randomise the values of the cells in grid 1 */
    sets all values to 0 and is missing a }
    which you added at the end of the program meaning the loops are all screwed up

    /* Make Grid 2 equal to Grid 1 */
    grid_2 [N][N] = grid_1 [N][N];
    sets the last single element in each grid to be the same
    also N is max and size is the final used element..

    with these fixed it runs fine. I could be bothered look through
    the algorithm to make sure the logic is correct. I assume that it is

    could do with adding a space or two to the printf statements
    to make it a bit more readable...


    Is this what you submitted and if so what mark did you get if you don't mind me asking?


  • Closed Accounts Posts: 285 ✭✭marauder


    Molly,
    what Hussey said is right in that you want to pass the whole array rather than 1 element however, your code won't work because you are passing the arrays by value. This means that a local copy of the conways and conways2 are created inside your secondary function. Any changes made to conway2 are lost when the function returns back to the main program.

    What you want to do is either pass by reference using pointers or simply make conways and conways2 global variables and don't pass anything to the function. That way the secondary function works on the real conways and can change them.

    you also have a } in the wrong place in the seconday function...
    the one before iarray++; in the while loop should be after it as iarray++ is outside your while loop and your program ends up in an infinite loop....


  • Registered Users, Registered Users 2 Posts: 950 ✭✭✭jessy


    marauder is wrong when he says that you are passing a copy of the array to the function because c dose not allow this to happen.
    in a c program a pointer to the array is all thats passed to the function some text books might make it look as though it is the entire array that is been passed but it is not.

    clearest method for passing an array.

    myarray(*array, MAX_ARRAY_SIZE);


  • Advertisement
  • Closed Accounts Posts: 285 ✭✭marauder


    Jessy,
    it is you who is wrong. c is a pass by value language. To pass by reference you need to use a pointer to pass the value of the address of the variable. This is what the * is in your given example...

    c.f.

    type the following into your favourite compiler....

    #include<stdio.h>
    
    
    void afunction(int);
    
    int main(void)
    {
    	int i;
    
    	i=5;
    	printf("i before afunction= %d \n",i);
    	afunction(i);
    	printf("i after afunction= %d \n",i);
    
    }
    
    void afunction(int j)
    {
    	j++;
    }
    

    if c was pass be reference then i would be 6 after the function call but it is still 5.....


    Here is how to pass by reference...
    #include<stdio.h>
    
    
    void afunction(int *);
    
    
    int main(void)
    {
    	int i;
    
    	i=5;
    	printf("i before afunction= %d \n",i);
    	afunction(&i);
    	printf("i after afunction= %d \n",i);
    
    }
    
    
    void afunction(int *j)
    {
    	(*j)++;
    }
    

    in this case i is 5 before and 6 after afunction...


  • Closed Accounts Posts: 14,483 ✭✭✭✭daveirl


    This post has been deleted.


  • Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭hussey


    maraunder is right and wrong.

    an array in c is essentially a pointer anyway .. An array is actually a pointer to the 0th element of the array. Dereferencing the array name will give the 0th element...

    Array element -> pointer equilivent
    arr[0] -> *arr
    arr[1]-> *(arr+1)
    arr[n]-> *(arr+n)

    you do not need to pass as a reference to change value :
    example
    #include<stdio.h>
    
    
    void afunction(int[]);
    
    int main(void)
    {
    	int i[2];
    
    	i[0]=5;
    	i[1]=4;
    	printf("i before afunction= %d \n",i[0]);
    	afunction(i);
    	printf("i after afunction= %d \n",i[0]);
    
    }
    
    void afunction(int j[])
    {
    	j[0]++;
    }
    

    the result I got using Borland command line complier was :

    i before afunction= 5
    i after afunction= 6


  • Registered Users, Registered Users 2 Posts: 950 ✭✭✭jessy


    marauder

    Sorry to continue on with this point, you were correct in saying that c is a pass by value language I am well aware of this but in my answer you don’t pass the actual pointer of the array to the function, you pass a copy of it “pass by value” but it is effectively pass by reference which is the only way you can pass an array to a function in c. If you find an advanced book on c it will tell you this. I am 100% positive that you can only pass a pointer to an array to a function.


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Originally posted by jessy
    If you find an advanced book on c it will tell you this. I am 100% positive that you can only pass a pointer to an array to a function.

    If anyone finds a book on C (or C++ for that matter) that doesn't tell you this intended for beginners (i.e. it doesn't reasonably assume you already know this) please post this fact to the board so we all know never to read it.


  • Advertisement
Advertisement