ページ 11

c言語

Posted: 2017年7月13日(木) 01:06
by mooooook
c言語初心者でmain関数内にある最初のprintfで表示される2つの文字列と、2回目のprintfで表示される2つの文字列が入れ替わっているようにしたく、その為、2組の文字列の内容を入れ替える関数を作成したいのですが関数記述と呼び出しがわからないです
教えていただけると助かりますお願いします。
#include<stdio.h>
#include<math.h>

『関数記述』

int main()

char a[10]=”book”;
char b [10]=”amazon”;
printf(”a:%s, b:%s¥n”,a,b);

『関数呼び出し部分』

printf(”a:%s, b:%s¥n”,a,b);
return(0);
}

Re: c言語

Posted: 2017年7月13日(木) 05:49
by かずま
mooooook さんが書きました:c言語初心者でmain関数内にある最初のprintfで表示される2つの文字列と、2回目のprintfで表示される2つの文字列が入れ替わっているようにしたく、その為、2組の文字列の内容を入れ替える関数を作成したいのですが関数記述と呼び出しがわからないです
ヤフー知恵袋との関係を教えてください。

Re: c言語

Posted: 2017年7月13日(木) 23:20
by box

コード:

// こんな感じなのかな~という気がしないでもないような気がする
#include <stdio.h>
#include <string.h>

#define N (10)

void irekae(char (*a)[N], char (*b)[N])
{
    char t[N];

    strcpy(t, *a), strcpy(*a, *b), strcpy(*b, t);
}

int main(void)
{
    char a[N] = "book";
    char b[N] = "amazon";

    printf("a:%s, b:%s\n", a, b);
    irekae(&a, &b);
    printf("a:%s, b:%s\n", a, b);
    return 0;
}