0

How do these both work? I would think the second one wouldn't work because when it is called here copy_string(line, line1) since it does not have a &. It also does not use a pointer in the function header copy_string(char line[], char line1[])

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void manipulateStrings(char *line[], char line1[])
{

    strcpy (line, line1);
}


int main(int argc, char *argv[])
{
    char line[3] = {0};
    char line1[80] = "hi";
    manipulateStrings(&line, line1);
    printf("line is %s \n", line);
    printf("line1 is %s \n", line1);
    return 0;
}

Surprised this one works.

#include <stdio.h>
#include <string.h>

void copy_string(char line[], char line1[]) {

    strcpy (line, line1);
}

int main() {
    char line[3] = {0};
    char line1[80] = "hi";
    copy_string(line, line1);
    printf("line is %s \n", line);
    printf("line1 is %s \n", line1);
    return 0;

   return 0; 
}
cokedude
  • 379
  • 1
  • 11
  • 21
  • 4
    The second one is correct. The first is expecting an array of pointers as first argument (notice it is the same as the `argv` argument for `main`). Turn on your compiler warnings. – Weather Vane Apr 08 '16 at 08:27
  • Any idea why this didn't complain about it http://ideone.com/LkDDnr? I usually use this for quick code snippets. – cokedude Apr 08 '16 at 08:29
  • because it only shows you the warnings when you have an error: http://ideone.com/sigKqx – BeyelerStudios Apr 08 '16 at 08:30
  • @WeatherVane like this right? http://stackoverflow.com/questions/1088622/how-do-i-create-an-array-of-strings-in-c/1095006#1095006 – cokedude Apr 08 '16 at 08:37
  • @BeyelerStudios thank you for showing me that trick with ideone. – cokedude Apr 08 '16 at 08:39

0 Answers0