C语言 将字符串值分配给指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5698353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
assign a string value to pointer
提问by hkvega
char *tempMonth;
char month[4];
month[0]='j';
month[1]='a';
month[2]='n';
month[3]='tempMonth=month;
';
how to assign month to tempMonth? thanks
如何将月份分配给 tempMonth?谢谢
and how to print it out finally?
以及如何最终打印出来?
thanks
谢谢
回答by the wolf
In C, month == &month[0](in most cases) and these equals a char *or character pointer.
在 C 中,month == &month[0](在大多数情况下)这些等于一个char *或字符指针。
So you can do:
所以你可以这样做:
char month[]="jan";
This will point the unassigned pointer tempMonthto point to the literal bytes allocated in the other 5 lines of your post.
这将指向未分配的指针tempMonth指向您帖子的其他 5 行中分配的文字字节。
To make a string literal, it is also simpler to do this:
要制作字符串文字,这样做也更简单:
char *month="jan";
Alternatively (though you're not allowed to modify the characters in this one):
或者(尽管您不允许修改此字符中的字符):
printf("That string by golly is: %s\n", tempMonth);
The compiler will automatically allocate the length of the literal on the right side of the month[]with a proper NULL terminated C string and monthwill point to the literal.
编译器将自动分配右侧的文字长度,month[]并使用适当的以 NULL 结尾的 C 字符串,month并将指向文字。
To print it:
要打印它:
tempmonth = month;
You may wish to review C strings and C string literals.
您可能希望查看C 字符串和 C 字符串文字。
回答by paxdiablo
If you just want a copy of the pointer,you can use:
如果您只想要指针的副本,可以使用:
tempmonth = strdup (month);
// Check that tempmonth != NULL.
but that means both point to the same underlying data - change one and it affects both.
但这意味着两者都指向相同的基础数据 - 更改一个,它会影响两者。
If you want independent strings, there's a good chance your system will have strdup, in which case you can use:
如果你想要独立的字符串,你的系统很有可能会有strdup,在这种情况下你可以使用:
char *strdup (const char *s) {
char *d = malloc (strlen (s) + 1); // Allocate memory
if (d != NULL) strcpy (d,s); // Copy string if okay
return d; // Return new memory
}
If your implementation doesn'thave strdup, get one:
如果您的实施不具备strdup,得到一个:
tempMonth = month
For printing out strings in a formatted fashion, look at the printffamily although, for a simple string like this going to standard output, putsmay be good enough (and likely more efficient).
要以格式化的方式打印字符串,请查看printf系列,尽管对于像这样进入标准输出的简单字符串,puts可能已经足够好(并且可能更有效)。
回答by littleadv
tempmonth = malloc (strlen (month) + 1); // allocate space
strcpy (tempMonth, month); //copy array of chars
When you assign a value to a pointer - it's a pointer, not a string. By assigning as above, you won't miraculously have two copies of the same string, you'll have two pointers (monthand tempMonth) pointing to the samestring.
当您为指针赋值时 - 它是一个指针,而不是一个字符串。通过如上分配,您不会奇迹般地拥有同一个字符串的两个副本,您将有两个指针 (month和tempMonth) 指向同一个字符串。
If what you want is a copy - you need to allocate memory (using malloc) and then actually copy the values (using strcpyif it's a null-terminated string, memcpyor a loop otherwise).
如果您想要的是副本 - 您需要分配内存(使用malloc),然后实际复制值(strcpy如果它是以空字符结尾的字符串,则使用,memcpy否则使用循环)。
回答by Freaktor
include <string.h>
Remember to:
记得:
#include "string.h" // or #include <cstring> if you're using C++
char *tempMonth;
tempMonth = malloc(strlen(month) + 1);
strcpy(tempMonth, month);
printf("%s", tempMonth);
回答by Andrew Rasmussen
char *months[] = {"Jan", "Feb", "Mar", "Apr","May", "Jun", "Jul", "Aug","Sep","Oct", "Nov", "Dec"};
回答by cMinor
You could do something like:
你可以这样做:
printf("%s\n", months[0]);
and get access
并获得访问权限
##代码##
