簡體   English   中英

在結構體數組中輸入char *

[英]Taking input of char* in array of struct

struct student
{
       char *name;
       int roll_no;

};

student s1[3];

for(int i=0;i<3;i++)
{
        cout<<"Enter name: ";
        cin.getline(s1[i].name,'\n');
        cout<<"\nEnter roll number : ";
        cin>>s1[i].roll_no;        

}  

我想在“ char *名稱”中輸入全名,但是我知道這不起作用,我可以使用字符串,但是有什么辦法可以使用char *進行此操作嗎?

在struct student中,您定義了指向char的指針,但沒有為其分配內存。 你需要類似的東西

#define STRSIZE 255
struct student
{
       char name[STRSIZE];
       int roll_no;

};
...
cin.getline(s1[i].name, STRSIZE);
...

getline的第二個arg是輸入緩沖區的長度,而不是定界符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM