Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

C language string pointer to do function parameter example analysis

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "C language string pointer to do function parameter example analysis", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "C language string pointer to do function parameter example analysis"!

See what's wrong with this code below?# include "stdio.h"

//#include "stdbool.h"

#include "string.h"

#include "stdlib.h"

#include "math.h"

void getMemory(char *p)

{

/*char *p = str*/

p = (char *)malloc(100);

strcpy(p,"hello world");

printf("p:%s\n",p);

}

int main()

{

printf("Enter main...\ n");

char *str = NULL;

printf("str:%p\n",str);

getMemory(str);

printf("%s\n",str);

if(str != NULL)

free(str);

return (0);

}

We look directly at the output, and the output looks like this.

Many people are not particularly clear about function parameters.

void getMemory(char *p)

{

/*char *p = str*/

p = (char *)malloc(100);

strcpy(p,"hello world");

printf("p:%s\n",p);

}

getMemory(str);

str is a pointer variable, that is, it stores a memory address, and this memory address points to the type char *"that is, string"

But when str is passed to getMemory (char * p), it passes a copy of str, not itself.

Since the copy is passed, the code operating in getMemory is also operating on this copy. When the function call ends, it is destroyed and recycled.

So str is still NULL.

How to modify?# include "stdio.h"

//#include "stdbool.h"

#include "string.h"

#include "stdlib.h"

#include "math.h"

void getMemory(char **p)

{

/*char **p = &str*/

*p = (char *)malloc(100);

strcpy(*p,"hello world");

printf("p:%s\n",*p);

}

int main()

{

printf("Enter main...\ n");

char *str = NULL;

printf("str:%p\n",str);

getMemory(&str);

printf("%s\n",str);

if(str != NULL)

free(str);

return (0);

}

Look at the output.

At this point, I believe that we have a deeper understanding of "C language string pointer to do function parameter example analysis," may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report