In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-15 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.