Advertisement

Vararg and how to use it?

Started by August 09, 2017 09:41 AM
7 comments, last by h8CplusplusGuru 7 years, 3 months ago

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!

This looks like it explains it.

https://stackoverflow.com/questions/1657883/variable-number-of-arguments-in-c

Hello to all my stalkers.

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);

 

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);
}

 

True, fixed. Thanks guys!

"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.

Hello to all my stalkers.

Advertisement

Thank you for your input, it starts to pan out nicely now :) 

Should definitely use std string over char*

This topic is closed to new replies.

Advertisement