va_copy — портирование на Visual C++?

Вы только что сделали одну маленькую ошибку. Вы, вероятно, заметите, что вы не использовали k ни для чего, поэтому вы просто выполняли одну и ту же задачу k раз. Просто измените m <= word.length()-1 на m < word.length()-k.

import java.util.*;

public class MyClass {
public static void main(String args[]) {

    Scanner kbreader = new Scanner (System.in);

    System.out.print("Enter a word: ");
    String word = kbreader.nextLine();


    for ( int k = word.length(); k > 0; k--)
    {

        for (int m = 0; m < word.length()-k; m++)
        {
            System.out.print(word.charAt(m));
        }

        System.out.println();          
       }       
   }
 }
17
задан Community 23 May 2017 в 11:48
поделиться

2 ответа

Необходимо смочь сойти с рук просто делание уроки:

va_list apcopy = ap;

Это - технически непортативное и неопределенное поведение, но это будет работать с большинством компиляторов и архитектуры. В x86 соглашении о вызовах va_list с являются просто указателями в стек и безопасны скопировать.

14
ответ дан 30 November 2019 в 12:37
поделиться

Одна вещь, которую можно сделать, состоит в том, если Вам иначе не нужно эти vformat() функция, переместите ее реализацию в format() (непротестированная) функция:

#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <string>
#include <vector>


std::string format(const char *fmt, ...)
{
   va_list ap;

   enum {size = 1024};

   // if you want a buffer on the stack for the 99% of the time case 
   //   for efficiency or whatever), I suggest something like
   //   STLSoft's auto_buffer<> template.
   //
   //   http://www.synesis.com.au/software/stlsoft/doc-1.9/classstlsoft_1_1auto__buffer.html
   //
   std::vector<char> buf( size);

   //
   // where you get a proper vsnprintf() for MSVC is another problem
   // maybe look at http://www.jhweiss.de/software/snprintf.html
   //

   // note that vsnprintf() might use the passed ap with the 
   //   va_arg() macro.  This would invalidate ap here, so we 
   //   we va_end() it here, and have to redo the va_start()
   //   if we want to use it again. From the C standard:
   //
   //       The object ap may be passed as an argument to
   //       another function; if that function invokes the 
   //       va_arg macro with parameter ap, the value of ap 
   //       in the calling function is indeterminate and 
   //       shall be passed to the va_end macro prior to 
   //       any further reference to ap.   
   //
   //    Thanks to Rob Kennedy for pointing that out.
   //
   va_start (ap, fmt);
   int needed = vsnprintf (&buf[0], buf.size(), fmt, ap);
   va_end( ap);

   if (needed >= size) {
       // vsnprintf reported that it wanted to write more characters
       // than we allotted.  So do a malloc of the right size and try again.
       // This doesn't happen very often if we chose our initial size
       // well.
       buf.resize( needed + 1);

       va_start (ap, fmt);
       needed = vsnprintf (&buf[0], buf.size(), fmt, ap);
       va_end( ap);

       assert( needed < buf.size());
   }

   return std::string( &buf[0]);
}
5
ответ дан 30 November 2019 в 12:37
поделиться
Другие вопросы по тегам:

Похожие вопросы: