Thread: How has this array been changed?

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    2

    How has this array been changed?

    // A UDF to copy a String

    #include <stdio.h>
    #include <conio.h>
    void stringcopy(char[],char[]);

    void main()
    {
    clrscr();

    char x[20];
    char y[20] = "Aquib Mir";

    stringcopy(x,y);

    printf("X is %s",x);

    getch();
    }

    void stringcopy(char s[20],char t[20])
    {
    int i = 0;
    while(t[i] != '\0')
    {
    s[i] = t[i];
    i++;
    }
    s[i] = '\0';
    }

    How has 'y' been copied to 'x'? Because I performed the operation in my own finction, not in main.?// A UDF to copy a String #include <stdio.h>

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Your function copies the second parameter to the first parameter, not the other way around. When you pass the array names to the function, it actually gets pointers to the address of the first element in the arrays. So the string copy function is actually acting on the arrays you passed, not copies of them.

    I don't fully understand what you are asking, perhaps you could reword it?

    p.s. - main returns an int

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    How has 'y' been copied to 'x'? Because I performed the operation in my own finction
    because array names are pointers, they store the address of the first element of the array.
    stringcopy(x,y);
    So what your doing here is telling your function to use the address's stored in x, y. So s and t are actually using the arrays x and y.

    [edit]
    Grrrrr beaten again
    [/edit]
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    2
    Thanks to both of you.

    Actually, I was trying to ask how the arrays in main were copied, when I performed the operation in my own function.

    Anyway, its clear now. I had the notion that the whole array is passed to the UDF. But, I now understand that the variables in the UDF are actually working as pointers. They are not receiving any array. The whole operation is actually performed in main.

    Thanks a lot.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >The whole operation is actually performed in main
    Actually, the operation is performed in your function on pointers that point to objects local to main.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  3. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  4. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  5. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM