_tcsdup()这个函数到底是干啥用的啊?

yingqichen 2010-02-24 11:32:39
看了半天MSDN,看不太懂,可能理解能力太差了,请假大家
能把这段下面这段代码翻译下最好不过了

CWinApp::CWinApp(LPCTSTR lpszAppName)
{
if (lpszAppName != NULL)
m_pszAppName = _tcsdup(lpszAppName);
else
m_pszAppName = NULL;
}


m_pszAppName这是个什么东西?
...全文
1739 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
hhzjzpp 2011-03-25
  • 打赏
  • 举报
回复
指定应用程序的名字。
yingqichen 2010-02-24
  • 打赏
  • 举报
回复
搞明白了 谢谢大家帮忙啊 实在是感激不尽!!!

WizardK 2010-02-24
  • 打赏
  • 举报
回复
复制字符串而已。
stjay 2010-02-24
  • 打赏
  • 举报
回复
没法修改帖子就是麻烦
LPTSTR buf = (LPTSTR)malloc((_tcslen(lpszAppName) + 1) * sizeof(TCHAR)); 
_tcscpy(buf, lpszAppName);

m_pszAppName = buf;
stjay 2010-02-24
  • 打赏
  • 举报
回复
m_pszAppName = _tcsdup(lpszAppName);
是分配字符串lpszAppName长度的内存,然后返回地址指针赋值给m_pszAppName
相当于

LPCSTR buf = (LPCSTR)malloc((_tcslen(lpszAppName) + 1) * sizeof(TCHAR));
_tcscpy(buf, lpszAppName);

m_pszAppName = buf;
stjay 2010-02-24
  • 打赏
  • 举报
回复
引用 7 楼 yingqichen 的回复:
引用 4 楼 visualeleven 的回复:The _strdup function calls malloc to allocate storage space for a copy of strSource and then copies strSource to the allocated space. 就是分配目的串内存空间,然后将源字符串的内容copy到目的串中。(加一句,用完以后记得要free掉目的串的内存)看看这个MSDN的例子就懂了: // crt_strdup.c #include <string.h> #include <stdio.h> int main( void ) {   char buffer[] = "This is the buffer text";   char *newstring;   printf( "Original: %s\n", buffer );   newstring = _strdup( buffer );   printf( "Copy:    %s\n", newstring );   free( newstring ); } 输出: Original: This is the buffer text Copy:    This is the buffer text

看懂了 m_pszAppName = _tcsdup(lpszAppName);这句代码的意思就是把lpszAppName这个字符串内容复制到变量m_pszAppName中 对吗


是分配字符串lpszAppName长度的内存,然后返回地址指针复制给m_pszAppName
相当于
char *buf = (char *)malloc(strlen(lpszAppName) + 1);
strcpy(buf, lpszAppName);

m_pszAppName = buf;
yingqichen 2010-02-24
  • 打赏
  • 举报
回复
引用 4 楼 visualeleven 的回复:
The _strdup function calls malloc to allocate storage space for a copy of strSource and then copies strSource to the allocated space.

就是分配目的串内存空间,然后将源字符串的内容copy到目的串中。(加一句,用完以后记得要free掉目的串的内存)
看看这个MSDN的例子就懂了:
// crt_strdup.c

#include <string.h>
#include <stdio.h>

int main( void )
{
  char buffer[] = "This is the buffer text";
  char *newstring;
  printf( "Original: %s\n", buffer );
  newstring = _strdup( buffer );
  printf( "Copy:    %s\n", newstring );
  free( newstring );
}

输出:
Original: This is the buffer text
Copy:    This is the buffer text


看懂了 m_pszAppName = _tcsdup(lpszAppName);这句代码的意思就是把lpszAppName这个字符串内容复制到变量m_pszAppName中 对吗
yingqichen 2010-02-24
  • 打赏
  • 举报
回复
引用 3 楼 jianghandaxue 的回复:
CWinApp::m_pszAppName

说明:
指定应用程序的名字。应用程序可以从传递给CWinApp的构造函数的参数中得到,如果其中没有指定名字,则是ID为AFX_IDS_APP_TITLE的资源字符串。如果在资源中找不到应用程序的名字,那么它来自程序的可执行文件名。全局函数AfxGetAppName返回该值。m_pszAppName是const char* 类型的公有变量。

需要慢慢理解 - -
yingqichen 2010-02-24
  • 打赏
  • 举报
回复
引用 2 楼 yhp1888 的回复:
看了半天MSDN,再花半小时查MSDN

The _strdup function calls malloc to allocate storage space for a copy of strSource and then copies strSource to the allocated space.

下面的懂吗?
int FuncA(LPCTSTR lpstr)
{
  if (lpszAppName != NULL)
      strcpy(m_pszAppName, lpstr);
  else
      m_pszAppName = NULL;
}

LPCTSTR,这个懂吗?

lpstr,这个懂吗?

相同的道理

有点懂了
Eleven 2010-02-24
  • 打赏
  • 举报
回复
The _strdup function calls malloc to allocate storage space for a copy of strSource and then copies strSource to the allocated space.

就是分配目的串内存空间,然后将源字符串的内容copy到目的串中。(加一句,用完以后记得要free掉目的串的内存)
看看这个MSDN的例子就懂了:
// crt_strdup.c

#include <string.h>
#include <stdio.h>

int main( void )
{
char buffer[] = "This is the buffer text";
char *newstring;
printf( "Original: %s\n", buffer );
newstring = _strdup( buffer );
printf( "Copy: %s\n", newstring );
free( newstring );
}

输出:
Original: This is the buffer text
Copy: This is the buffer text
jianghandaxue 2010-02-24
  • 打赏
  • 举报
回复
CWinApp::m_pszAppName

说明:
指定应用程序的名字。应用程序可以从传递给CWinApp的构造函数的参数中得到,如果其中没有指定名字,则是ID为AFX_IDS_APP_TITLE的资源字符串。如果在资源中找不到应用程序的名字,那么它来自程序的可执行文件名。全局函数AfxGetAppName返回该值。m_pszAppName是const char* 类型的公有变量。
yhp1888 2010-02-24
  • 打赏
  • 举报
回复
看了半天MSDN,再花半小时查MSDN

The _strdup function calls malloc to allocate storage space for a copy of strSource and then copies strSource to the allocated space.

下面的懂吗?
int FuncA(LPCTSTR lpstr)
{
if (lpszAppName != NULL)
strcpy(m_pszAppName, lpstr);
else
m_pszAppName = NULL;
}

LPCTSTR,这个懂吗?

lpstr,这个懂吗?

相同的道理
yingqichen 2010-02-24
  • 打赏
  • 举报
回复
自己顶顶 请大家帮帮忙

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧