1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[C]配列の一番最初の要素を削除する

Posted at

「配列の一番最初の値がいらないからそれを除いた配列を新たに作ってくれ」と言われたので実装したときのメモです。
Cには配列要素のdeleteなんぞ無いのでポインタを使ってわちゃわちゃするしかありません。

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

int main(void) {
    int a[5] = {114, 11, 14, 19, 1};
    int b[4];

    memcpy(b, a + 1, 4 * sizeof(int));

    for(int i = 0; i < 4; i++)
        printf("%d ", b[i]);

    return 0;
}

配列aのポインタを1個すすめて、そこから要素4個分コピーをするというやり方なんですが、もっといい方法がありそう。
もっといい書き方を知ってるという人を募集してます。

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?