Thread: macro function

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45

    macro function

    Hello everyone,
    I am stuck and confused about macros. I need to define a macro that returns the HOME dir of user. This is what I have so far:
    Code:
    #define GLADE_FILE { \
        char *homeDir = getenv("HOME"); \
        char *tempi = "/hello.glade"; \
        strcat(homeDir, tempi); \ 
        printf("%s", homeDir); \
    }
    as you can see it does not work. If I try to put it into a regular function and try to pass homeDir with return(homeDir)
    I get
    home-dir.c:19: warning: return makes integer from pointer without a cast
    Any help would be great,
    Thanks in advance.
    Brad

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You can't just strcat them together like that. I doubt getenv() will return enough of a buffer to guarentee that it will be safe.

    In reality, you should make this into a function imo.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Quote Originally Posted by bradleyd View Post
    Hello everyone,
    I am stuck and confused about macros. I need to define a macro that returns the HOME dir of user. This is what I have so far:
    Code:
    #define GLADE_FILE { \
        char *homeDir = getenv("HOME"); \
        char *tempi = "/hello.glade"; \
        strcat(homeDir, tempi); \ 
        printf("%s", homeDir); \
    }
    as you can see it does not work. If I try to put it into a regular function and try to pass homeDir with return(homeDir)
    I get
    Any help would be great,
    Thanks in advance.
    Brad
    Code:
    #define PATHLEN 256 
    
     char mypath[PATHLEN] = { '\0' };
     char *homeDir = getenv("HOME");
     strncpy(mypath, homeDir, PATHLEN);
     strncat(mypath, "/hello.glade", PATHLEN - strlen(mypath));
     printf("%s", mypath);
    Last edited by Brian; 05-20-2007 at 08:26 PM. Reason: '\0' not 0

  4. #4
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    I tried to put it into a function then call the function but I get that error. Here is the function:
    Code:
    char hd(void)
    {
    	char *homeDir = getenv("HOME");
        char *temp = "/hello.glade";
        strcat(homeDir, temp);
        return(homeDir);
    }
    if I substitute return(homeDir) with printf("%s",homeDir);
    it works fine.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you just want to print it without any error checking, then you could do something like this:

    Code:
    #define printglade() printf("%s/hello.glade",getenv("HOME"))
    Since getenv() can return NULL, this is probably not a good idea.

    If you need to return a string:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char *getGlade(void);
    
    int main(void)
    {
    	char *szGlade = getGlade();
    	
    	if(szGlade)
    	{
    		printf("Glade directory: <%s>\n",szGlade);
    		free(szGlade);
    	}
    	else perror("Unable to retrieve Glade directory.");
    	
    	return 0;
    }
    
    char *getGlade(void)
    {
    	char *homeDir,*temp = "/hello.glade",*szReturn = NULL;
    	
    	homeDir = getenv("HOME");
    	if(homeDir)
    	{
    		szReturn = malloc(strlen(homeDir) + strlen(temp) + 1);
    		if(szReturn)
    		{
    			strcpy(szReturn,homeDir);
    			strcat(szReturn,temp);
    		}
    	}
    	return szReturn;
    }

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by bradleyd View Post
    I tried to put it into a function then call the function but I get that error. Here is the function:
    Code:
    char hd(void)
    {
    	char *homeDir = getenv("HOME");
        char *temp = "/hello.glade";
        strcat(homeDir, temp);
        return(homeDir);
    }
    if I substitute return(homeDir) with printf("&#37;s",homeDir);
    it works fine.
    Your supposed to return a char pointer (char *) not a char, see MacGyver's post.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    Thanks that worked, I kinda foggy on char *GetHome vs. char GetHome. If you want to return any string from a function do you always have use a pointer in declaring the function?
    when you want to return a int you just int SomeNumber(void).
    Just having trouble grasping that.
    Thanks,
    Brad

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    A string is a set of chars that are sequential in memory, ending with a final '\0' char. One way of representing such a data type can be done using a char array. A char array, down deep under the hood, is represented simply by a pointer to the first element of the array. This means to return a string, you have to return a char *. If you return a char, you only return one character.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    ok that makes sense, thanks MacGyver.
    Pointers seem to be my weak point in C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM