Thread: Problems using a function

  1. #16
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    783
    Quote Originally Posted by zach View Post
    Is the pass back occurring by means of scanf - does the response to scanf result in placement of the value in memory, leading to the value becoming available in main() also?
    The latter. The scanf results in the memory you passed being written to. That memory "lives" in main so of course the change is visible there.

    If you insist on creation an array inside a function and returning it, you'll usually want to allocate heap memory (malloc).

  2. #17
    Registered User
    Join Date
    Aug 2019
    Location
    Groningen Netherlands
    Posts
    104
    Salem, what I don't understand is how the character array, present in main, gets into the function, without being listed as a parameter of the call. As I understand it, instead, a pointer to the array is in the call, but the array is in the parameter list of the array. And how does str get back from the function to main? I find this rather spooky.

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,376
    Do you understand how str gets passed to scanf here, and how the result gets back into the local array 'str'?
    Code:
    int main(void)
    {
        char str[5];
        printf("Enter security code: ");
        scanf("%4s", str);
        printf("Code=%s\n",str);
        return 0;
    }
    This is no different.
    Code:
    void security(char []);
    int main(void)
    {
        char str[5];
        security(str);
        printf("Code=%s\n",str);
        return 0;
    }
      
    void security(char str[])
    {
        printf("Enter security code: ");
        scanf("%4s", str);
    }
    There is only ONE array, and that's the one declared in main.
    Every other function down the chain is just dealing with a pointer to that array.

    If you really want to emphasis the "pointer" side of things, you could equally have written.
    Code:
    void security(char *);  // absolutely no difference with char[]
    int main(void)
    {
        char str[5];
        security(str);
        printf("Code=%s\n",str);
        return 0;
    }
      
    // The name you use here has NO relation to the name(s) 
    // of any parameters you choose to call it with
    void security(char *word) 
    {
        printf("Enter security code: ");
        scanf("%4s", word);
    }
    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.

  4. #19
    Registered User
    Join Date
    Aug 2019
    Location
    Groningen Netherlands
    Posts
    104
    Salem, my confusion derived from the nomenclature in my query. Apologies for keeping on about one thing for so long. Thank you for your patience. Your explanation is excellent. Zach K.

  5. #20
    Registered User
    Join Date
    Aug 2019
    Location
    Groningen Netherlands
    Posts
    104
    Quote Originally Posted by Structure View Post
    wtf ?
    Yes what abut it?

  6. #21
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    117
    Quote Originally Posted by zach View Post
    Yes what abut it?
    I was hoping you knew.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function Problems....
    By C+noob in forum C++ Programming
    Replies: 9
    Last Post: 07-11-2005, 04:37 PM
  2. sin() function problems.
    By Lifedragn in forum C Programming
    Replies: 4
    Last Post: 09-28-2004, 11:16 PM
  3. function problems
    By money in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2003, 11:40 AM
  4. function problems
    By faxtoaster in forum C Programming
    Replies: 5
    Last Post: 07-13-2003, 06:25 PM
  5. function problems
    By $0.05$ in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2003, 11:03 PM

Tags for this Thread