Char指针 - 如何赋予价值? [英] Char pointer - How to give value?
问题描述
我刚刚开始学习C并且正在研究指针,但是我很难对以下内容感到困难:
如果我有一个结构,包括一个指向char的指针(在我的情况下,我已经将b $ b命名为文件名),在主代码中,我想使用那个指针
来创建一个内存位置并分配一个字符串。
我在主代码中得到了以下内容(structPtr是一个指针
到他们创建的结构类型):
structPtr-> filename = malloc(10 * sizeof(char));
>
如何给出一个值,让逗号保持字符串值为cust.txt。到这个
字段?
非常感谢任何帮助,
谢谢,
>
Alij
Hi,
I have just started learning C and am working on pointers but am
having difficulty with the following:
If i have a struct, that includes a pointer to char (in my case, i''ve
named it filename), and in the main code, I want to use that pointer
to create a memory location and assign a character string.
I have gotten the following in the main code (structPtr is a pointer
to they type of struct i have created):
structPtr->filename = malloc(10 * sizeof(char));
How do I give a value, lets stay string value of "cust.txt" to this
field?
Will appreciate any help,
Thanks,
Alij
推荐答案
3月19日下午6:49,alij < aliasger.jaf ... @ gmail.comwrote:
On Mar 19, 6:49 pm, "alij" <aliasger.jaf...@gmail.comwrote:
我刚开始学习C和我正在研究指针,但是我很难用以下内容:
如果我有一个结构,它包含一个指向char的指针(在我的情况下,我'$
将其命名为filename),在主代码中,我想使用该指针
来创建一个内存位置并分配一个字符串。
我在主代码中得到了以下内容(structPtr是一个指针
到他们创建的结构类型):
structPtr-> filename = malloc(10 * sizeof(char));
如何给出一个值,让逗号保持字符串值为cust.txt到这个
字段?
Hi,
I have just started learning C and am working on pointers but am
having difficulty with the following:
If i have a struct, that includes a pointer to char (in my case, i''ve
named it filename), and in the main code, I want to use that pointer
to create a memory location and assign a character string.
I have gotten the following in the main code (structPtr is a pointer
to they type of struct i have created):
structPtr->filename = malloc(10 * sizeof(char));
How do I give a value, lets stay string value of "cust.txt" to this
field?
strcpy(structPtr-> filename," cust.txt");
顺便说一下,sizeof( char)总是1,所以malloc(10)会这样做。
strcpy(structPtr->filename,"cust.txt");
By the way, sizeof(char) is always 1, so malloc(10) will do.
感谢任何帮助,
谢谢,
Alij
Will appreciate any help,
Thanks,
Alij
3月19日下午1:58,Francine.Ne ...... @ googlemail.com写道:
On Mar 19, 1:58 pm, Francine.Ne...@googlemail.com wrote:
3月19日下午6:49,alij < aliasger.jaf ... @ gmail.comwrote:
On Mar 19, 6:49 pm, "alij" <aliasger.jaf...@gmail.comwrote:
您好,
Hi,
我刚刚开始学习C并正在研究指针,但是我很难用以下内容:
I have just started learning C and am working on pointers but am
having difficulty with the following:
如果我有一个结构,包括一个指向char的指针(在我的情况下,我已经将b $ b命名为文件名),在主代码中,我想使用那个指针
用于创建内存位置并分配字符串。
If i have a struct, that includes a pointer to char (in my case, i''ve
named it filename), and in the main code, I want to use that pointer
to create a memory location and assign a character string.
我在主代码中得到以下内容(structPtr是一个指针
到他们创建的结构类型):
I have gotten the following in the main code (structPtr is a pointer
to they type of struct i have created):
structPtr-> filename = malloc(10 * sizeof(char));
structPtr->filename = malloc(10 * sizeof(char));
如何给出一个值,让逗号保持字符串值为cust.txt到这个
字段?
How do I give a value, lets stay string value of "cust.txt" to this
field?
strcpy(structPtr-> filename," cust.txt");
顺便说一下,sizeof( char)总是1,所以malloc(10)会这样做。
strcpy(structPtr->filename,"cust.txt");
By the way, sizeof(char) is always 1, so malloc(10) will do.
感谢任何帮助,
Will appreciate any help,
谢谢,
Thanks,
Alij
Alij
谢谢为了你的帮助!!
Thank you for your help!!
alij写道:
alij wrote:
我刚刚开始学习C并且正在研究指针,但是我很难用以下方法:
如果我有一个struct,包括指向char的指针(在我的情况下,我已经将b $ b命名为文件名),在主代码中,我想使用那个指针
创建一个内存位置并分配一个字符串。
我在主代码中得到了以下内容(structPtr是一个指针
到他们创建的结构类型):
structPtr-> filename = malloc(10 * sizeof(char));
Hi,
I have just started learning C and am working on pointers but am
having difficulty with the following:
If i have a struct, that includes a pointer to char (in my case, i''ve
named it filename), and in the main code, I want to use that pointer
to create a memory location and assign a character string.
I have gotten the following in the main code (structPtr is a pointer
to they type of struct i have created):
structPtr->filename = malloc(10 * sizeof(char));
^^^^^^^^^^^^^^
sizeof(char)的定义是1。以上是打字练习到最后。
^^^^^^^^^^^^^^
sizeof(char) is 1 by definition. The above is typing practice to no end.
>
我如何给出一个值,让逗号保持字符串值为cust.txt到这个
字段?
>
How do I give a value, lets stay string value of "cust.txt" to this
field?
#include< stdio.h>
#include< stdlib.h>
#include < string.h>
#define FIELDSIZE 10
#define FILENAME" cust.txt"
struct foo
{
char * filename;
};
int main(void )
{
struct foo thething,* structPtr =& thething;
if(!(structPtr-> filename) = malloc(FIELDSIZE))){
printf(" malloc failed。我退出!\ n);
退出(EXIT_FAILURE);
}
if(strlen(FILENAME)> = FIELDSIZE){
printf(" \"" FILENAME" \")太长了。我退出了!\ n;
免费(structPtr->文件名);
退出(EXIT_FAILURE);
}
strcpy(structPtr-> filename,FILENAME);
printf(" structPtr-> filename is \"%s \" \ n"
∧ andthing .filename是\"%s \" \ n",
structPtr-> filename,thething.filename);
free(structPtr-> ; filename);
返回0;
}
structPtr-> filename是cust.txt
和thething.filename是cust.txt
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIELDSIZE 10
#define FILENAME "cust.txt"
struct foo
{
char *filename;
};
int main(void)
{
struct foo thething, *structPtr = &thething;
if (!(structPtr->filename = malloc(FIELDSIZE))) {
printf("malloc failed. I quit!\n");
exit(EXIT_FAILURE);
}
if (strlen(FILENAME) >= FIELDSIZE) {
printf("\"" FILENAME "\" is too long. I quit!\n");
free(structPtr->filename);
exit(EXIT_FAILURE);
}
strcpy(structPtr->filename, FILENAME);
printf("structPtr->filename is \"%s\"\n"
" and thething.filename is \"%s\"\n",
structPtr->filename, thething.filename);
free(structPtr->filename);
return 0;
}
structPtr->filename is "cust.txt"
and thething.filename is "cust.txt"
这篇关于Char指针 - 如何赋予价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!