Hi,


I have decalared a structure named record as given below and also created a function called read() inside the structure. The function works fine when I call it using this statement:

myRecord.read();

but I want to create one more function which can be called to initilize all the variables and arrays.

When I tried to initialize an array inside the structure, it gave an warning message message that "for loop... can't be expanded inline."

What should I do to initiliza arrays defined inside a structure?

When I write the user input to a text file, garbage values are also written. So, I want to initialize arrays before using them.

Inside the given below structure if I create a function similar to the read() one, it doesn't work and wranings come:

void init() {

int count = 0; //This initialization works fine.

for(count = 0; count < 100; count++) {

 a[count] = ' ';

}

}

Can anybody help me?

void main() {

	clear;

	typedef struct record {

		char 	q[100];
		char 	a1[100];
		char 	a2[100];
		char 	a3[100];
		char 	a4[100];


		void read() {

		       printf("\nQuestion: ");
		       gets(q);

		       printf("\nA: ");
		       gets(a1);

		       printf("\nB: ");
		       gets(a2);

		       printf("\nC: ");
		       gets(a3);

		       printf("\nD: ");
		       gets(a4);

		       //printf("\nChoice: ");
		       //scanf("%d",can);
		};


}record;


	record myRecord;
	myRecord.read();
}

Dani AI

Generated

Quick summary: the garbage you see comes from using raw, uninitialized fixed-size char buffers and then writing the whole buffer (or otherwise reading past the terminating NUL). As pointed out, your code is C++ (C doesn't allow member functions inside a struct), so use C++ patterns. The compiler warning about the for‑loop being "can't be expanded inline" is an optimization/inlining notice caused by defining the function body inside the class (member definitions inside a class are implicitly inline). It is a compile-time optimization warning, not a runtime error — move the function definition outside the class or make it a constructor to avoid it.

Safer, modern fix (recommended): stop using fixed char arrays and gets(). Use std::string and std::getline — strings are zero-initialized and avoid buffer overflows and leftover garbage when writing to files. Example pattern:

struct Record {
  std::string q, a1, a2, a3, a4;
  void read() {
    std::getline(std::cin, q);
    std::getline(std::cin, a1);
    // ...
  }
};

If you must keep C-style char arrays: either zero-initialize them at declaration (C++11 and later: char q[100] = {};) or ensure full zeroing in a constructor/initialiser before use (e.g. std::fill_n or memset). Also avoid gets() — use fgets() with a size limit in C, or std::getline in C++. When writing to file, write only up to the string terminator (fprintf/fputs or write strlen(buf) bytes), not the full fixed buffer, unless you intentionally want fixed-width records.

Quick troubleshooting checklist:

  • Replace void main() with int main() and return 0.
  • Replace gets() with safe input routines.
  • Prefer std::string (simplest fix).
  • If keeping char arrays, either zero-initialize at declaration or zero them in a constructor; define an init() method outside the struct to avoid inline expansion warnings.
  • When writing, use functions that respect the NUL terminator (fprintf/fputs/ostream::operator<<) or pass the actual length.

Recommended Answers

All 3 Replies

What is "clear"?

Your program is not a C program. It is C++. I think it is better that this post would moved to C++ section.


Inside a class or structure (both are almost same thing in C++) you can not set member values because they are not still created.

Well, it's so simple (in C++ ;) ):

struct Record {
    char q[100];
    char a1[100];
    ...
    Record() {
        memset(q,0,sizeof q);
        memset(a1,0,sizeof a1);
        ...
    }
   void read() {
   ...
};
....
Record YourRecord; ...

You can also intialize the arrays in main but outside structure,if you don't want to use inbuilt function memset() .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.