Article 6ET2E pass buffer from function to main()

pass buffer from function to main()

by
justwantin
from LinuxQuestions.org on (#6ET2E)
I'm traveling in unfamiliar territory ... usually I can sort my c programming problems by searching on the net but I have not been able to sort this one out. I'm using linux/gcc. I have a rather long program that works but I want to break it down somewhat with functions. In this instance I want to pass a 'hours:minutes' time stamp to main(). I can print the time stamp from the function but not from main(). Code:rick@rpi6:./testf
in main
in function
temp_buf is: 13:25
Segmentation faultI will provide the program below. In the above the buffer is not printed in main, but is printed in the function and then there is a segmentation fault. Note the long list of includes is what is required for the complete program. I am not sure if the final function 'free(temp_buf)' is either needed or does anthing.Code:// Using math.h so must use -lm at end of gcc command
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>

char *get_time()
{
char *temp_buf = malloc(7);
time_t curtime;
struct tm* tm_info;
time(&curtime);
tm_info = localtime(&curtime);
strftime(temp_buf, 7, "%H:%M", tm_info);
printf("in function\n");
printf("temp_buf is: %s\n", temp_buf);
return temp_buf;
}
int main()
{
printf("in main\n");
char *temp_buf = get_time();
printf("buffer is: %s\n", *temp_buf);
// free(buffer);
return 0;
}
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments