简体   繁体   中英

C - use scanf to scan string into an array

I am trying to make a program that uses one scansf function to get someones entire name at once and stores each letter in one layer of an array. For example what it should do if the name entered is "tim":

 array[0]=t
 array[1]=i
 array[2]=m
 array[3]=\0

i have read this in other threats but it doesn´t work:

#include <stdio.h>

int main(void) {
    char array[256];
    scanf_s("%s", array);
    printf_s("%s", array);
    return 0;
}

It gives out this error message:

在此处输入图片说明

English:
Exeption triggered at 0x0FAA0B5C
access violation while writing position 0x01300000

Is it even possible to use the scanf function if yes how? Or is there a better alternative?


Thank you by adding the buffer parameter works
code:

#include <stdio.h>
#include "stdafx.h"

int main(void) {
    char array[256];
    scanf_s("%255s", array, 256);
    printf("%s", array);
    return 0;
}

simply use scanf

#include <stdio.h>

int main(void) {
    char array[256];
    scanf("%s", array);
    printf("%s", array);
    return 0;
}

It is possible to use scanf without the _s . Eg:

scanf("%s",array);
printf("%s\n", array);

如果您想使用scanf_s,那么请看这里

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM