0

I'm having a really hard time adjusting function to my needs. First of all look at those three files and notice how I have to call f_texture function in main function in order to make it work:

externs.h

#ifndef EXTERNS_H_
#define EXTERNS_H_

extern char t_about[100];
extern int friction;

extern int f_texture(char* ,char*);

#endif

functionA.c

#include <stdio.h>
#include "externs.h"

int main()
{
    f_texture("rough","friction");
    printf("Friction: %d\n", friction);
    f_texture("rough","t_about");
    return 0;
}

functionB.c

#include "externs.h"
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
char t_about[100];
int friction;

int f_texture(char* texture,char* what_is_needed)
{
    /*Checking if both values are present*/
    assert(what_is_needed);
    assert(texture);

    /*Static array in order to prevent it's disappearance*/
    memset(t_about, '\0', sizeof(t_about));

    /*Configuring variables for desired texture*/
    if (strcmp(texture, "smooth") == 0)
    {
        strcpy(t_about, "The surface is perfectly smooth, without any "
            "protuberances.\n");
        friction = 0;
    }
    else if (strcmp(texture, "rough") == 0)
    {
        strcpy(t_about, "Rough bumps can be feeled under my fingertips.\n");
        friction = 4;
    }
    /*In case of absent keyword of desired texture it will crash the program*/
    else
    {
        assert(!what_is_needed);
    }

    /*Returning desired value*/
    if (strcmp(what_is_needed, "t_about") == 0)
    {
        int i=0;
        while (t_about[i] != '\0')
            {
            printf("%c", t_about[i]);
            i++;
            }
    }
    else if (strcmp(what_is_needed, "friction") == 0)
    {
        return friction;
    }
    /*In case of absent keyword of desired value it will crash the program*/
    else
    {
        assert(!what_is_needed);
    }

    return 0;
}

And now here is my question: How to rewrite this code to make it possible to call f_texture function without using quotation marks inside? I mean instead of f_texture("abcd","efgh") just to type f_texture(abcd,efgh). I've noticed that this way it's required just after I've wrote this code. Thanks in advance.

Winged
  • 261
  • 2
  • 4
  • 11
  • you want to call it **f_texture()** only? – Deidrei Dec 13 '13 at 09:14
  • Do you want to do something like that `f_texture(rough, friction);` ? – jmlemetayer Dec 13 '13 at 09:15
  • save the desired string to some variable, then pass that variable to f_texture(). – sujin Dec 13 '13 at 09:17
  • You can't or the only way is to declare a `char rough[6] = "rough";` and a `char friction[9] = "friction";` before. Or to use some macros. – jmlemetayer Dec 13 '13 at 09:18
  • @sujin But as far as I can imagine that, I would have to save every single possible string value to a another variable in order to make it work the way you are proposing, and that would be very impractical IMO. – Winged Dec 13 '13 at 09:22
  • @user3080141: you shouldn't use string literal directly. It's better to declare (const) variable to hold the string's content you want to use. – Deidrei Dec 13 '13 at 09:25
  • @user3080141 go for macro or array of string or array of string with enum will be more readable. – sujin Dec 13 '13 at 09:28

4 Answers4

2

If you don't want to assign string constants to variables or preprocessor object macros, another option is to use preprocessor function macros, using the stringification feature:

#define call_f_texture(a,b) f_texture(#a,#b)
....
call_f_texture(rough,friction);

The C preprocessor will turn this into

f_texture("rough","friction");
Mark Plotnick
  • 9,598
  • 1
  • 24
  • 40
1

You can also use some macros:

#define ROUGH    "rough"
#define FRICTION "friction"
#define T_ABOUT  "t_about"

int main()
{
    f_texture(ROUGH, FRICTION);
    printf("Friction: %d\n", friction);
    f_texture(ROUGH, T_ABOUT);
    return 0;
}
jmlemetayer
  • 4,774
  • 1
  • 32
  • 46
  • This is exactly what I was looking for! And it's so simple that I feel akward due to not figuring it out by myself ;x BTW is there a way to define it more automaticly rather than typing every single definition? E.g. something like defining space inside this function rather than defining elements which can be put inside. – Winged Dec 13 '13 at 09:29
  • You can use some technics like [the arguments](http://gcc.gnu.org/onlinedocs/cpp/Macro-Arguments.html#Macro-Arguments) or [concatenation](http://gcc.gnu.org/onlinedocs/cpp/Concatenation.html#Concatenation) to create some automatic macros. But I think you will always have to define some fixe macro for the beginning of your automatitation. – jmlemetayer Dec 13 '13 at 09:35
0

You can do like this,

char rough[]="rough";
char friction[]= "friction";

and call

f_texture(rough, friction);
Chinna
  • 3,930
  • 4
  • 25
  • 55
0
char a[MAX] = "rouch";
char b[MAX} = "friction";

int main()
{
      f_texture();
      ...
}

int f_texture()
{
    /*Checking if both values are present*/
    assert(b);
    assert(a);
}

or

int f_texture(char* a,char* b)
{
    /*Checking if both values are present*/
    assert(b);
    assert(a);
    ...
}

int main()
{
    char a[MAX] = "rouch";
    char b[MAX} = "friction";
    f_texture(a,b);
    ...
}