How to omit quotation marks usage in char type?

161 views Asked by At

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.

4

There are 4 answers

2
jmlemetayer On BEST ANSWER

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;
}
0
Chinna On

You can do like this,

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

and call

f_texture(rough, friction);
0
AudioBubble On
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);
    ...
}
1
Mark Plotnick On

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");