Jump to content
  • Advertisement
Sign in to follow this  
suliman

Vararg and how to use it?

Recommended Posts

Hi!
I need help with how to write a function that takes any no of char arrays. It's involved with "..." param i know but im confused on the exact syntax:

manyWrite (100, 100, "First text", "Second text"); // call the function like this

manyWrite (int x, int y, ...){
	//loop over the infinite arguments :)
	myWrite(x,y + index*20, argument[index]);
}

Many thanks!

Share this post


Link to post
Share on other sites
Advertisement

Thanks!

This one seemed less messy to me than the c-style vararg dance:

std::vector<char *> test;
test.push_back("line one");
test.push_back("line two");
manyWrite (100, 100, test);
  
//declaration
manyWrite (int x, int y, std::vector <char *> lines);

 

Edited by suliman

Share this post


Link to post
Share on other sites
13 minutes ago, suliman said:

Thanks!

This one seemed less messy to me than the c-style vararg dance:


std::vector<char *> test;
test.push_back("line one");
test.push_back("line two");
manyWrite (100, 100, test);
  
//declaration
manyWrite (int x, int y, std::vector <char *> lines);

 

Please add at least a reference operator, right know you are copy the entire vector by value for each call.

 

But regarding your original question - You have to pass the count along on every call, there is no safe-way to loop over a dynamic array, even when you know the type because when you iterated over the last item you dont know what memory comes after it:

void manyWrite(const int x, const int y, const int lineCount, ...) {
	va_list vargs;
	va_start(vargs, lineCount);
	for (int lineIndex = 0; lineIndex < lineCount; ++lineIndex) {
    	char *line = va_arg(vargs, char *);
        ...
	}
	va_end(vargs);
}

 

Edited by Finalspace

Share this post


Link to post
Share on other sites

"std::vector<char*> * content" -- this means that "content" is a pointer to a vector somewhere else (because of the bolded asterisk), and that you have to manage this stuff somehow. (e.g. new and delete, and using with pointer dereferencing) I doubt this is what you want.

You probably want something like this (with less magic numbers than your example):

std::vector<char *> content;
content.push_back("Test 1");
content.push_back("Test 2");
content.push_back("Test 3");
const int x = 100;
const int y = 100;
manyWrite (x, y, test);

void manyWrite(int startX, int startY, std::vector<char *> & lines)
{
  	const int verticalSpacing = 20;
	for (int i = 0; i < lines.size(); i++)
	{
		myWrite(startX, startY + (i * verticalSpacing), lines[i]);
	}
}

Alternatively, make it just a vector of std::string instead of char*, and use the "c_str" function of std::string when needed. Depends on how you're going to use the "manyWrite" function, though.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!