Re: How is memory allocated

Discussion in 'C Programming' started by Chris Dollin, Jun 27, 2003.

  1. Chris Dollin

    Chris Dollin Guest

    Samuel Thomas wrote:

    > #include <stdio.h>
    > #include <conio.h>

    Non-standard header. You probably don't need this.

    > void printnamefirst(char[]);
    > void printnamesec(char[]);
    > void main()

    `main` is required to return `int`.

    > {
    > clrscr();

    Non-standard function. You almost certainly don't need this [1].

    > printnamefirst(name);
    > }

    Undeclared variable `name`. This is beginning to look like homework ...

    > void printnamefirst(char nm[])

    `nm` is not a helpful name.

    > {
    > char nam[20] ="Samuej Thomas";
    > printnamesec(nam);
    > printf("%s \n",nam);

    So, what's the point of `nm`?

    > }
    > void printnamesec(char ns[])
    > {
    > printf("%s \n",ns);
    > ns[5]='l';
    > }
    > 1.Is it safe to use the variables that are allocated in one function,
    > in another function as I have done by printing a string in the
    > printnamesec, but which has been declared in printnamefirst function?

    It's not safe, because you can't do it. You're not using the variables;
    you're using the values. BUT, since an array is almost always converted
    into a pointer to its first element, you can't use a reference to an
    array [variable] once the block that declares that variable has been
    finished with.

    > 2.Is it possible to make 'pass by value' work with character strings
    > so that they dont get changed? Do they always get passed as reference
    > values when passed across functions?

    C does not have pass by reference at all. What it does have is that arrays
    will usually decay into pointers to their first element. The effect is
    similar BUT thinking of it as pass-by-reference will catch you out sometime,

    > Does the value of the nam
    > variable declared in printnamefirst get modified because of the 'pass
    > by reference' mechanism?

    No; it's because the reference to `nam` as a function argument got
    converted into a pointer. This happens just about *anywhere*; it's
    nothing to do with argument-passing.

    [1] Some people regard unnecessary screen-clearing as a Serious Sin.
    I don't treat it so lightly.

    Chris "electric hedgehog" Dollin
    C FAQs at:
    C welcome:
    Chris Dollin, Jun 27, 2003
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Duncan
    Jul 21, 2003
  2. Laura Heinzmann

    How to check memory size allocated to JVM?

    Laura Heinzmann, Feb 16, 2005, in forum: Java
    John McGrath
    Feb 16, 2005
  3. Kjell Arne Johansen

    How do I know if memory is already allocated?

    Kjell Arne Johansen, Sep 1, 2003, in forum: C++
    Kjell Arne Johansen
    Sep 2, 2003
  4. C++fan
    Jack Klein
    Jan 7, 2004
  5. Replies:
    Matt Wharton
    Dec 9, 2004