Thread: incompatable type for argument 1 of enter address

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    incompatable type for argument 1 of enter address

    i have this code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct
    {
        int door_number;
        char street;
        char town;
        char county;
        char postcode;
    }Address;
    
    void enter_address(Address *p_address)
    {
        p_address->door_number = 1;
        //etc etc
    }
    int main()
    {
        int i;
        Address book[20];
    
        for (i = 0; i < 20; i++)
        {
           enter_address(book[i]);
        }
        return 0;
    }
    i know the solution is to call enter address like this
    Code:
     enter_address(book + entrys)
    but what i don't understand is why since array[1] is the same as array + 1

    is this just another example of c stands for contrary
    Last edited by cooper1200; 05-18-2019 at 03:18 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Because [] dereferences.

    > but what i don't understand is why since array[1] is the same as array + 1
    Except it isn't.

    book is an array.

    In the context of calling a function, enter_address(book) is just the same as enter_address(&book[0])

    In short, enter_address(book+i) and enter_address(&book[i]) are the same thing, and what you want.

    enter_address(book[i]) is something completely different.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    Quote Originally Posted by Salem View Post
    In short, enter_address(book+i) and enter_address(&book[i]) are the same thing, and what you want.
    ok i think that is where i was getting confused
    coop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 11-25-2014, 12:30 PM
  2. Replies: 3
    Last Post: 10-23-2013, 12:28 PM
  3. Passing address or pointer as argument to function
    By Edelweiss in forum C Programming
    Replies: 7
    Last Post: 08-17-2011, 12:38 AM
  4. Replies: 5
    Last Post: 01-24-2011, 05:37 AM
  5. Replies: 1
    Last Post: 03-24-2008, 10:16 AM

Tags for this Thread