#General Questions About C!

48 messages · Page 1 of 1 (latest)

woven mist
#

Can you guys help me answer these questions?

Which of the following statements are correct?

  1. Structure variables are passed to a function on a call-by-reference basis
  2. The value of a float variable is copied to the local variable when it is passed to a procedure.
    variable when it is passed to a procedure.
  3. The expression &x returns a reference to the variable x.
  4. Functions cannot be passed as a reference to a procedure
quiet pecanBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

grand pebble
#

what do you think is the right one

crystal gale
woven mist
grand pebble
#

and why are the others wrong ?

crystal gale
#

also are 'procedure' and 'function' used interchangeably

grand pebble
#

does the std mention procedures ?

crystal gale
# grand pebble does the std mention procedures ?

The procedures used to develop this document and those intended for its further maintenance are
described in the ISO/IEC Directives, Part 1. In particular, the different approval criteria needed for
the different types of document should be noted. This document was drafted in accordance with the
editorial rules of the ISO/IEC Directives, Part 2 (see www.iso.org/directives).
Foreword Paragraph 2 C17

grand pebble
#

lol

crystal gale
#

that's the only time in the document it says 'procedure'

grand pebble
#

c functions are technically procedures and not functions in a mathematical sense iirc ?

crystal gale
#

though that doesn't say very much, for example the standard never says 'rvalue' except in a footnote even though that is a common term

crystal gale
grand pebble
#

did i miss one ?

#

ah the 2nd one

#

but i dont like the word prodecure there TROLL

woven mist
#

I think that statements 1 and 2 can be the case, but does not have to

grand pebble
#
  1. no, C doesnt have call by reference
  2. yes, the value of a float is copied (pass by value), i however dont like the wording procedure tho
  3. yes
  4. no, functions can be passed as a reference to another function (also prodecure here)
woven mist
grand pebble
#

in C everything is pass by value

crystal gale
#

like it would be in other languages

grand pebble
#

even pointers are passed as values

woven mist
#

But what is &a then?

crystal gale
#
struct X{
    int x;
};
void foo(struct X x){
    x.x=42;
}
struct X x={0};
foo(x);
//x.x is still 0
crystal gale
#

you're passing the pointer by value

woven mist
#

But what about this?

#include <stdio.h>

typedef struct node {
    char data;
    struct node *next;
} Node;

typedef struct {
    size_t size;
    Node *head, *tail;
} LinkedList;

LinkedList init() {
    LinkedList l;

    l.size = 0;
    l.head = NULL;
    l.tail = NULL;

    return l;
}

bool isEmpty(LinkedList *l) {
    return l->head == NULL;
}

int main(void) {
    LinkedList l = init();

    // pass by reference
    isEmpty(&l);

    return 0;
}
crystal gale
#

so it's pass by value

woven mist
crystal gale
#

it just so happens that the value references something else

crystal gale
#

called a pointer

grand pebble
#

if it would pass by reference this would be possible

void alloc(int *arr) {
    arr = malloc(...)
}
#

it isnt tho

#

because it makes a copy of the arr variable
and modifies a local copy, not affecting the variable outside of it

crystal gale
#

the difference is that in other languages, it will implicitly pass a pointer, and you may even need to make an explicitly copy to get something different

woven mist
grand pebble
#

no

#

;compile c

#include <stdio.h>
#include <stdlib.h>
void alloc(int *i) {
     printf("address of i in alloc() %p\n", (void*)&i);
    i = malloc(sizeof(*i) * 123);
    printf("%p inside fn call\n", (void*)i);
}
int main() {
    int *i = NULL;
    printf("%p     before fn call\n", i);
    printf("address of i in main()  %p\n", (void*)&i);
    alloc(i);
    printf("%p     after  fn call\n", (void*)i);
}
coarse flowerBOT
#
Program Output
(nil)     before fn call
address of i in main()  0x7ffdf8bcae68
address of i in alloc() 0x7ffdf8bcae48
0x1b402b0 inside fn call
(nil)     after  fn call
woven mist
#

!solved