Thread: Parameter Valdity

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

    Parameter Valdity

    Hey,

    given that code:

    Code:
    void call(int *a)
    {
      a = malloc(1 * sizeof(int));
      a[0] = 1;
    }
    
    int main()
    {
    
      int *k;
      call(k);
      printf("%d", k[0]);
      return 0;
    }
    0 gets printed out. Why? I think my intension is clear.
    Last edited by Salem; 06-02-2019 at 11:04 AM. Reason: Removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The 'a' at line 1 is a 'pass by value' copy of the pointer k at line 11.

    You need to write something like
    k = call();
    or
    call(&k);

    if you want the call function to actually update the variable in main.
    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
    May 2010
    Posts
    4,632
    Do you realize that that pointer is being passed by value? This means that any changes to the pointer will be lost when the function returns and in this case creates a memory leak.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    69
    Ahh, pass by value of course. Thank you.
    But what to do if you want to pass a whole array like this. (Secondary return type)
    Last edited by SuchtyTV; 06-02-2019 at 11:26 AM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    But what to do if you want to pass a whole array like this.
    What array are you talking about? I don't see any arrays in your code.

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    69
    Quote Originally Posted by jimblumberg View Post
    What array are you talking about? I don't see any arrays in your code.
    See here: 'Untitled Post' | TextUploader.com

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Perhaps you need to increase your compiler warning levels and fix all warnings and errors?

    ||=== Build: Debug in chomework (compiler: GCC 8-1) ===|
    main.c||In function ‘main’:|
    main.c|20|warning: ‘a_lengths’ is used uninitialized in this function [-Wuninitialized]|
    main.c|22|warning: ‘b_lengths’ is used uninitialized in this function [-Wuninitialized]|
    main.c|24|warning: ‘c_lengths’ is used uninitialized in this function [-Wuninitialized]|
    ||=== Build finished: 0 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 01-08-2008, 10:25 AM
  2. Getting parameter
    By brietje698 in forum C++ Programming
    Replies: 3
    Last Post: 09-08-2007, 03:04 PM
  3. Parameter Help
    By houssam_ballout in forum C++ Programming
    Replies: 7
    Last Post: 05-02-2006, 07:05 AM
  4. Parameter?
    By freak in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2005, 04:29 PM
  5. Bad Parameter ???
    By hostensteffa in forum C Programming
    Replies: 2
    Last Post: 06-18-2002, 12:57 AM

Tags for this Thread