Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pointers? why this gives different outputs 1

Status
Not open for further replies.

vcwanabe

Programmer
Jun 16, 2002
8
US
Please help me understand what is going on here. The two printfs give two differnt outputs, why. My goal is to pass the asciitime return to a string the main function can use.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//Funtion Declarations
void getCurDateTime(char* DT);

//Functions
int main()
{
  //Private variables
  char CurDT[80];        

  getCurDateTime(CurDT);   
  printf("%s\n",CurDT);  // diff. output than function     
               
  system("PAUSE");	
  return 0;
}

void getCurDateTime(char* DT)
{
     time_t now;
     struct tm *ptr;
     
     now = time(0);
     ptr = localtime(&now);
     DT = asctime(ptr);
     printf("%s\n",DT);   //output that i want in main
}
 
You can't assign a string result to an array just by assigning it to a pointer to that array.

Code:
strcpy(DT,asctime(ptr));
Should fix that.

Don't forget to
Code:
#include <string.h>

--
 
Thank you Salem, yes that fixed it:)

Could you find any faults in my reasoning that...

1)The char*(DT) is assigned the memory address returned by asciitime(&ptr)?
2)The printf statement below that assignment goes to that memory address to retrieve what it prints?
3)when the datetime function ends the memory address is no longer valid?
4) when the main function accesses that memory location to print it gets whatever junk my be there?
 
1)The char*(DT) is assigned the memory address returned by asciitime(&ptr)?
Yes

2)The printf statement below that assignment goes to that memory address to retrieve what it prints?
Yes

3)when the datetime function ends the memory address is no longer valid?
Well the memory address is valid, it's just that your only copy of the address (in DT) disappears at the end of the function. Parameters to functions are like initialised local variables (in terms of scope), so whatever value is stored in them disappears at the end of the function.

4) when the main function accesses that memory location to print it gets whatever junk my be there?
Since DT was only ever a copy of the pointer to the start of your array, the array itself remains untouched by any change to DT inside the function you called.
Which is why I used strcpy() to copy the string to where DT was pointing (the array) so that the array itself was updated prior to the function returning.

--
 
As an addition:
There is no reason to use a statically
alloc'd array in this code.
A pointer to type char is all you need.
Code:
int main()
{
  //Private variables
  char *CurDT = NULL;       

  CurDT = getCurDateTime(CurDT);   
  printf("%s\n",CurDT);  // diff. output than function     
               
  system("PAUSE");    
  return 0;
}

char *getCurDateTime(char* DT)
{
     time_t now;
     struct tm *ptr;
     
     now = time(0);
     ptr = localtime(&now);
     if (DT != NULL) {printf("Possible mem leak and possible continuing ugliness or segv\n"; exit(1);) 
     DT = asctime(ptr);
     printf("%s\n",DT);   //output that i want in main
     return DT;
}
 
That way is also valid, but passing DT as a parameter is a waste of effort.
Code:
char *getCurDateTime(void);
int main()
{
  //Private variables
  char *CurDT = NULL;       

  CurDT = getCurDateTime();   
  printf("%s\n",CurDT);
               
  system("PAUSE");    
  return 0;
}

char *getCurDateTime(void)
{
     time_t now;
     struct tm *ptr;
     char *DT;
     
     now = time(0);
     ptr = localtime(&now);
     DT = asctime(ptr);
     printf("%s\n",DT);
     return DT;
}

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top