gpt4 book ai didi

c - 测试 strstr 是否返回有效指针

转载 作者:行者123 更新时间:2023-11-30 14:20:15 25 4
gpt4 key购买 nike

我正在尝试使用这样的 strstr 函数:

const char * pInicio = strstr(origem, chave);       
if (pInicio != NULL) ...

与预期相反,当我进行“if”测试时,编译器抛出访问位置冲突异常。每当我尝试从字符串中获取一些不存在的关键字时,都会发生这种情况。

调用堆栈是:

    ntdll.dll!779315de()    
[Os quadros abaixo podem estar incorretos e/ou ausentes, nenhum símbolo foi carregado para ntdll.dll]
ntdll.dll!779315de()
ntdll.dll!7792014e()
> msvcr100d.dll!_output_l(_iobuf * stream, const char * format, localeinfo_struct * plocinfo, char * argptr) Linha 2361 + 0xa bytes C++
cccccccc()

输出是:

Exceção de primeira chance em 0x0f8620a7 (msvcr100d.dll) no TestesApp.exe: 0xC0000005: Violação de acesso ao ler o local 0x00000001.
Exceção sem-tratamento em 0x779315de no TestesApp.exe: 0xC0000005: Violação de acesso ao ler o local 0x00000001.

就在异常发生之前,变量值为:

  1. chave: 0x00196f64 "$GNGSA"类型:char *
  2. Origem: 0x00196840 "$GPRMC,000651.082,V,,,,,,,191210,,,N*4F $GPGGA,000652.091,,,,,0,00,,,M,0.0,M,,0000 *5F $GPGSA,A,1,,,,,,,,,,,,,,,*1E"类型:const char *
  3. pInicio: 0x00000000 无效> 类型:const char *

链接器命令行是:

/OUT:"C:\Users\Leandro\documents\visual studio 2010\Projects\TestesApp\Debug\TestesApp.exe" /INCREMENTAL /NOLOGO "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /MANIFEST /ManifestFile:"Debug\TestesApp.exe.intermediate.manifest" /ALLOWISOLATION /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"C:\Users\Leandro\documents\visual studio 2010\Projects\TestesApp\Debug\TestesApp.pdb" /SUBSYSTEM:CONSOLE /PGD:"C:\Users\Leandro\documents\visual studio 2010\Projects\TestesApp\Debug\TestesApp.pgd" /TLBID:1 /DYNAMICBASE /NXCOMPAT /MACHINE:X86 /ERRORREPORT:QUEUE 

c/c++ 命令行也是

/ZI /nologo /W3 /WX- /Od /Oy- /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Gm /EHsc /RTC1 /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fp"Debug\TestesApp.pch" /Fa"Debug\" /Fo"Debug\" /Fd"Debug\vc100.pdb" /Gd /analyze- /errorReport:queue 

此后,我尝试测试指针是否为空。这就是我现在的问题,如何解决?

最诚挚的问候!

最佳答案

无法复制您的错误。这是我尝试过的:

int main( int argc, char **argv )
{
char * chave = "$GNGSA";
const char * origem = "$GPRMC,000651.082,V,,,,,,,191210,,,N*4F $GPGGA,000652.091,,,,,0,00,,,M,0.0,M,,0000*5F $GPGSA,A,1,,,,,,,,,,,,,,,*1E";
const char * pInicio = strstr(origem, chave);
if (pInicio != NULL)
{
printf("found it\n");
}
return 0;
}

这在 Visual Studio 2010 中编译和运行时没有错误。顺便说一句,它没有找到匹配项。我愿意接受有关如何调整它以创建错误的说明。

--编辑--

将编译器命令行设置为:/ZI/nologo/W3/WX-/Od/Oy-/D "WIN32"/D "_D​​EBUG"/D "_CONSOLE"/D "_UNICODE"/D "UNICODE"/Gm/EHsc/RTC1/GS/fp:精确/Zc:wchar_t/Zc:forScope/Fp"调试\odt.pch"/Fa"调试\"/Fo"调试\"/Fd"调试\vc100.pdb"/Gd/analyze-/errorReport:队列

链接器命令行:/OUT:"E:\code\odt\Debug\odt.exe"/INCRMENTAL/NOLOGO "kernel32.lib""user32.lib""gdi32.lib""winspool.lib""comdlg32 .lib""advapi32.lib""shell32.lib""ole32.lib""oleaut32.lib""uuid.lib""odbc32.lib""odbccp32.lib"/MANIFEST/ManifestFile:"Debug\odt.exe。 middle.manifest"/ALLOWISOLATION/MANIFESTUAC:"level='asInvoker' uiAccess='false'"/DEBUG/PDB:"E:\code\odt\Debug\odt.pdb"/SUBSYSTEM:CONSOLE/PGD:"E:\code\odt\Debug\odt.pgd"/TLBID:1/DYNAMICBASE/NXCOMPAT/MACHINE:X86/ERRORREPORT:QUEUE

即使两个命令行本质上相同,我仍然没有收到错误。我不知道该说什么,抱歉。

关于c - 测试 strstr 是否返回有效指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15644321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com