Forum: Compiler & IDEs sprintf produziert Warning funktioniert jedoch


von Andreas K. (koczwara)


Lesenswert?

Hallo,

mich macht die sprintf-Funktion ein wenig kirre, denn beim Compilieren 
wird bei jedem Aufruf von sprintf ein Warning (siehe unten) ausgeworfen.
Ich habe mein Problem hier auf die wesentlichen Zeilen reduziert. Die 
Funktionsaufrufe funktionieren jedoch einwandfrei, aber ein Warning ist 
meines erachtens nie zu ignorieren ... ich wüßte schon gern warum ..

Vielleicht hat ja jemand eine Idee oder ne Lösung.

Schonmal Danke fürs Lesen

Andreas


Verwendete Software:
Windows xp prof.
AVR Studio 4.14.589
GUI Version 4, 14, 0, 589
GCC Version WinAVR-20080610

--------

#include <stdio.h>

void sprintf_Test(unsigned int Ausgabewert)
{
  unsigned char Zeile_1[20];
  sprintf(Zeile_1, "Wert = %d", Ausgabewert);
}


int main(void)
{
  sprintf_Test(23);
}

--------

avr-gcc.exe  -mmcu=atmega32 -Wall -gdwarf-2 -std=gnu99 -O0 
-funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP 
-MT sprintf.o -MF dep/sprintf.o.d  -c  ../sprintf.c
../sprintf.c: In function 'sprintf_Test':
../sprintf.c:8: warning: pointer targets in passing argument 1 of 
'sprintf' differ in signedness
Build succeeded with 1 Warnings...

von Johannes M. (johnny-m)


Lesenswert?

Textstrings sind in C per Definitionem char und nicht unsigned char 
und darauf weist der Compiler Dich hin. sprintf möchte seine Ausgabe 
gerne in ein Array of char schreiben, Du gibst aber ein Array of 
unsigned char an. In diesem Fall hat das keine Auswirkungen auf den 
Programmablauf, da Textzeichen prinzipiell vorzeichenlos betrachtet 
werden, aber solche Sachen können zu Kollisionen führen.

von Oliver (Gast)


Lesenswert?

>warning: pointer targets in passing argument 1 of 'sprintf' differ in  signedness

ist doch eigentlich ziemlich selbsterklärend:

>of 'sprintf'
Es geht um sprintf()

>passing argument 1
es geht um das erste Argument von sprintf

>pointer targets ...
der übergebene Pointer verusacht das Problem

>differ in  signedness
das Problem ist ein signed/unsigned-Unterschied.

Schau dir den Funktionsprototypen von sprintf() nochmal an.

Oliver

von Andreas K. (koczwara)


Lesenswert?

Danke für die schnelle Hilfe, manchmal ist man ganz schön Betriebsblind.

Schönes Wochenende

Andreas

von Stefan D. (Gast)


Lesenswert?

Hallo

Ich habe ein ähnliches Problem.
Ich lege einen char Buffer an und erhalte dann bei der sprintf Ausgabe 
folgende Warnung:
"Warning [2066] type qualifier mismatch in assignment"


void main()
{
char  buffer [17];
....
....
....
sprintf (buffer, "Test");
....
....
....
}


Kann mir jemand erklären wie ich die Warnung weg bekomme oder eine 
Quelle nennen die mir weiter hilft?


Schon mal vielen Dank
Stefan

von Stefan D. (Gast)


Lesenswert?

Sorry
Ich habe gerade gesehen, das ich im falschen Forum bin. Ich verwende den 
C18 Compiler von Microchip (MPLAB).

von Stefan B. (stefan) Benutzerseite


Lesenswert?

>> Hinweis: der Originalbeitrag ist mehr als 6 Monate alt.

> char  buffer [17];
> sprintf (buffer, "Test");
> "Warning [2066] type qualifier mismatch in assignment"
> bei C18 Compiler von Microchip (MPLAB)

Welchen type qualifier erwartet der C18 Compiler von Microchip für die 
Funktion sprintf()? Schau dir den Prototypen von sprintf in stdio.h an.

[zitat http://www.kevin.org/frc/C18_2.4_users_guide.pdf]
2.7.4 stdio.h Functions
The output functions defined in stdio.h differ from the ANSI defined 
versions with regards to data in program memory, floating-point format 
support, and MPLAB C18 specific extensions.
The functions puts and fputs expect the output string to be stored in 
program memory. The functions vsprintf, vprintf, sprintf, printf, 
fprintf and vfprintf expect the format string to be stored in program 
memory.
The functions vsprintf, vprintf, sprintf, printf, fprintf and vfprintf 
do not support floating-point conversion specifiers.
The MPLAB C18 specific extensions for 24-bit integers and data in 
program memory are described in Section 4.7 of MPLAB® C18 C Compiler 
Libraries.
[/zitat]

Dies würde das 2. Argument "test" betreffen. Jedoch steht in der 
Anleitung auch:

[zitat]
2.7.3 String Constants
The primary use of data located in program memory is for static strings. 
In keeping with this, MPLAB C18 automatically places all string 
constants in program memory. This type of a string constant is “array of 
char located in program memory”, (const rom char []).
[/zitat]

d.h. die Bedingung für sprintf() wäre erfüllt.

Schnüffelt man weiter, kommt das an die Oberfläche:

[zitat http://www.microchip.com/forums/tm.aspx?m=53856&mpage=1]
The compiler libraries are built with the large memory model (far rom 
pointers) while the compiler uses the near memory model (near rom 
pointers) by default, unless you compile with -ml. The compiler is 
complaining that you're passing a 16-bit pointer to a string constant to 
a function that expects a 24-bit pointer. The compiler knows how to 
perform that conversion, so the code will work correctly even with the 
warning. Most any random char* cast applied to the string literal will 
suppress the warning while still generating correct code. The correct 
cast is (const rom far char *), but incorrect casts like (char *) will 
work, and even meaningless casts like (near far rom rom rom char *) will 
work.
[zitat]

In dem Thread dort sind auch verschiedene Lösungen angegeben.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.