-2

So basically strcpy assigns the address of the 2nd argument to the 1st, but how does it do it with an array as the first argument? like in my program, i tried changing the address of the array but unfortunately it wont compile. So I had to resort to making a character pointer variable to assign the return value of capitalize. Is there something I'm misunderstanding?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char string[20];
char *Capitalize(char *str)
{
    int i;
    char *temp;
    temp = malloc(sizeof(char)*(int)(strlen(str)+1));
    for(i = 0;i < strlen(str);i++)
    {
        if(*(str+i) >= 'a' && *(str+i)<= 'z')
            *(temp+i) = *(str+i) - 32;
        else
            *(temp+i) = *(str+i);
    }
    *(temp+i) = '\0';
    return temp;
}
int main(void)
{
    string word;
    printf("Enter word to capitalize: ");
    scanf("%19s",word);
    word = Capitalize(word);
    printf("%s",word);
    return 0;
}
jantristanmilan
  • 4,188
  • 14
  • 53
  • 69
  • 4
    "strcpy assigns the address of the 2nd argument to the 1st" no it doesn't, it copies the contents. – Mat Oct 02 '12 at 17:14
  • 1
    Why are you asking about strcpy() when none of your example code uses it? – Glenn Oct 02 '12 at 17:15
  • @Glenn word = Capitalize(word) this gives an error, so I wondered how strcpy() assigns the address of the 2nd argument to an array, when apparently it can't. I'm practicing strings so I suddenly pondered on the idea. – jantristanmilan Oct 02 '12 at 17:17
  • 1
    As a side note, `sizeof(char)` is defined to be 1 and the cast is useless, so you can change `malloc(sizeof(char)*(int)(strlen(str)+1));` to `malloc(strlen(str) + 1);` – Seth Carnegie Oct 02 '12 at 17:18
  • You are assigning `char *` to `char [20]`, that is the problem – another.anon.coward Oct 02 '12 at 17:19

3 Answers3

1

strcpy() makes a copy, just like the name implies. it's perfectly legal to copy a string in to an array.

When you make an initialization of an array such as:

char myarr[] = "hello";

You're actually copying the characters into the array.

You seem to be confusing arrays with pointers (see here for some reason you can't treat them the same)

Community
  • 1
  • 1
Mike
  • 47,263
  • 29
  • 113
  • 177
0

The thing you seem to be missing is that in c/c++ strings ARE arrays, in most practical respects declaring

char c[] = "hello";

and

char* c = "hello";

is the same thing, all strcpy does is copy the characters into the destination memory, whether that memory is allocated as an array (presumably on the stack) or pointer (presumably on the heap);it does not make a difference.

Community
  • 1
  • 1
tletnes
  • 1,958
  • 10
  • 30
  • 1
    No, the two declarations are very different. The first declares a `char[6]` named `c`, which you can modify, but you can't assign to, `c = whatever;` won't compile. The second declares a pointer and initialises it to point to an _unmodifiable_ (well, it might be modifiable, but attempting to do so invokes undefined behaviour) `char[6]`, you can then assign a different address to the pointer later if you desire. – Daniel Fischer Oct 02 '12 at 17:35
  • No they're not the same. In the first, `c` is an array, in the second `c` is a pointer to read-only memory. Also, in C++, strings are not arrays (or at least, they shouldn't be). – netcoder Oct 02 '12 at 17:36
  • "in most practical respects" Also I'm not sure where you get the idea that it is a pointer to read only memory, that is implmentation defined. – tletnes Oct 02 '12 at 19:13
0

In C, qualifying an array by name without an indexer, is equivalent to specifying a pointer to the memory address of the first element in the array, that is why you can pass as a parameter an array to functions like strcpy.

char * strcpy ( char * destination, const char * source );

strcpy will copy whatever series of characters are found, starting at memory address specified by source, to the memory address specified by destination, until a null character (0) is found (this null character is also copied to the destination buffer).

The address values specified in the parameters are not modified, they just specify from where in memory to copy and where to. It is important that destination is pointing to a memory buffer (can be a char array or a block of memory requested via malloc) with enough capacity for the copied string to fit, otherwise a buffer underrun will occur (you will write characters past the end of your buffer) and your program might crash or behave in a weird way.

Hope I have been clear and not confused you more with my explanation ;)