Jump to content
Sign in to follow this  
pengguna_komputer

Gets() Error

Recommended Posts

Satu lagi soklan basic..

saya mengompilkan source code dibawah menggunakan gcc.

#include <stdio.h>
main()
{
  char szNama[75];
  printf("Sila masukkan nama anda\n");
  gets(szNama);
  printf("Apakhabar %s\n", szNama);
  return 0;
}
ia akan menghasilkan error seperti dibawah, tetapi program masih boleh digunakan.
cc mygets.c
/tmp/ccA6hNRP.o(.text+0x34): In function `main':
: warning: the `gets' function is dangerous and should not be used.

Kalu ader alternatif lain, sila kongsi...

Share this post


Link to post
Share on other sites

kau pakai compiler apa? aku run program kau tu in VS 6.0 and VS 7.3.. takde masalah.

in u're code. char szNama[75];

an input longer than 74 characters will overrun the szNama buffer and almost certainly cause the program to crash.

also, read up on fgets

Edited by da^hype

Share this post


Link to post
Share on other sites

Thanks pada suma yang bantu n bagi hints kat saya

Hasil ujikaji cket, gets memang tak secure dan boleh menyebabkan buffer overrun, so fgets digunakan untuk atasi masalah ni...

Kod dibawah ni menggunakan fgets.

#include <stdio.h> 
main()
{
	char szNama[75];
	printf("Sila masukkan nama anda\n");
	fgets(szNama,75,stdin);
	printf("Apakhabar %s", szNama);
	return 0;
} 

Boleh dikompil tanpa warning msg.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...