如何使用指针和字符在 C 中询问用户输入

发布于 2025-01-12 16:15:03 字数 534 浏览 4 评论 0原文

#include<stdio.h>
int main(){
    char *p;

    printf("User:- Please write about my self here --> \n");
    scanf("%s",p);
    //Case 1
    printf("The o/p of first persion is here -->>\n %s",p);
    //This gives output as (null) Can you please tell why it's happening

    // Case 2
    printf("\n The op here is -->> %p\n",p);
    //This gives op as (nil)
}

你能向我解释一下吗?或者删除一些指针链接,这是我第一次了解堆栈和指针 提前致谢。参考案例 1、2

#include<stdio.h>
int main(){
    char *p;

    printf("User:- Please write about my self here --> \n");
    scanf("%s",p);
    //Case 1
    printf("The o/p of first persion is here -->>\n %s",p);
    //This gives output as (null) Can you please tell why it's happening

    // Case 2
    printf("\n The op here is -->> %p\n",p);
    //This gives op as (nil)
}

Can you please explain to me or drop some links for pointer it's my first time on Stack and pointers
Thanks in advance.Refer to case 1, 2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揽月 2025-01-19 16:15:03

它给出 null 因为你没有初始化 p,你没有给它任何指向的地址。你刚刚宣布了它。使用 malloc() 或为其提供另一个 char 数组的地址。

char array[nindex];
p = array;
As array stores address of first element

Or you malloc()
p = malloc(size);

It is giving null because you not initilized p, you not gave it any address to point. You just declared it. Use malloc() or give it the address of another array of char.

char array[nindex];
p = array;
As array stores address of first element

Or you malloc()
p = malloc(size);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文