-1

So I'm trying to copy part of an array into another array in the simplest way possible. I was trying to avoid using a loop. This was my thought process...

char date[]="20140805";
char year =date[0..3];

The ".." is what is causing the error. I want to be able to break up the date variable into parts, and was hoping to be able to do so compactly in one line like this. Some help would be appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
halolord01
  • 173
  • 1
  • 3
  • 8

3 Answers3

5

You should not use a loop.

char year[5];
char date[] = "20140805";

memcpy(year, date, 4);
year[4] = 0;

that's how you should do it, or may be you want

char date[] = "20140805";
char year[] = {date[0], date[1], date[2], date[3], 0};
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • This is a string, so why not string functions? – crashmstr Dec 15 '14 at 18:22
  • Is `strncpy(year,date,4);` wrong? with year as `char year[];`? – Gopi Dec 15 '14 at 18:31
  • 1
    @Gopi, yes. `char year[];` is of *unknown size*, and no terminator is appended. – Weather Vane Dec 15 '14 at 18:35
  • @crashmstr not `strncpy`. It states clearly as a Warning that i the first `n` characters of the string don't contain the null byte you must add it. to quote the manual _Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated._ – Iharob Al Asimi Dec 15 '14 at 18:37
  • @IharobAlAsimi Pickiness note for creating good habits: The `0` in `year[4] = 0;` and the `0` in the last line (char year[] = {..., 0}) may be interpreted as an `int` instead of a `char`, creating a type mismatch warning. __Solutions__: replace `0` with `(char)0` or `'\0'` or `(char)NULL`. Notice that `'\0'` doesn't need to be casted to an int because its type is _already_ char; using `'\0'` would be the most correct way, but others may yield better readability (depending on your coding experience). All three are equivalent afaik. – Stev Aug 03 '23 at 10:54
  • `(char) NULL`? And FYI `'\0'` has type `int`. – Iharob Al Asimi Aug 08 '23 at 13:28
1

Here is an example to do that :

In fact you can copy any part of a string using this method :)

just change the from and sz variable and you are done :)

#include <stdio.h>
#include <string.h>
int main ()
{
  char date[]= "20140805";
  int sz=4; // number of characters to copy

  char year[sz+1];
  int from = 0; // here from is where you start to copy

  strncpy ( year, date + from, sz );

  year[sz]=0;

  puts (year);

  return 0;
}
Ali Akber
  • 3,670
  • 3
  • 26
  • 40
  • You shouldn't use strncpy() anyway, it isn't a proper string function. – 2501 Dec 15 '14 at 18:32
  • If you are using C code, than what will you use rather than strncpy() ? @2501 of course if you use string in STL then another case – Ali Akber Dec 15 '14 at 18:34
0

OP wanted a one-liner: here in one declaration plus one line.

char year[5] = {0};
strncpy(year,date,4);

This answer addresses the weak point of strncpy() which does not append a final 0 if count <= strlen(source);. It's not the best solution but it answers OP's question while avoiding the trap.

Byte dumps of the char array before and after the strncpy()

0 0 0 0 0
50 48 49 52 0
Weather Vane
  • 33,872
  • 7
  • 36
  • 56