How has this array been changed?

This is a discussion on How has this array been changed? within the C Programming forums, part of the General Programming Boards category; // A UDF to copy a String #include <stdio.h> #include <conio.h> void stringcopy(char[],char[]); void main() { clrscr(); char x[20]; char ...

  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,796
    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
    523
    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,796
    >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, 05: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, 08:01 PM
  5. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 01:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21