Is there something similar to strcpy but for int value? - c

Thank you, it works ....................................................................
strcpy(holder[pos].key, new.key);
holder[pos].position = new.position
strcpy(holder[pos].name, new.name);
strcpy(holder[pos].country, new.country);

You don't need strcpy or any special function for that. Strcopy is only necessary because strings aren't strictly just a data type in C, but null-terminated char arrays.
You can do it like this:
holder[pos].position = new.position; // Assigns the value of new.position to holder[pos].position

please use
holder[pos].position = new.position

Related

C string array, assigning and passing etc

I have been rethinking my way of asking. so i edited the question.
i know beforehand how many strings there will be in the array because it is menu text.
suppose i have an array of strings declared as so:char *menu_item[4];
this should give me an array that can hold 4 strings right?
i want to be able to pass the string array to other functions so is this the way to do it?
This array is not declared in main. but in a function that is called from main.
will i have to allocate memory to use fx strcpy(menu_item[1],"some text");
and if yes. what should i allocate??
or is menu_item[0] = "some text"okay??
i have a function in a function to print out the strings which takes a char *string as parameter. and the function itself takes a string array as so char *items[] it looks like this:
void scroll_menu(char *items[], int size){
for(int i = 0; i < size; i++){
print_out(items[i]);
}
}
is the parameter correct for a string array?
i have been looking through a lot of question online. and cant seem to find anything that solves my problem.
if any doubt i will recap what it is i want:
i want to declare an array of strings which holds a known number of strings. sometimes the strings can be initialized in the declaration of the array. and other times i have to look up somewhere and then "assign" or copy the result to the array.
i want to be able to copy or assign strings to it from functions returning a char *string like so strcpy(menu_item[0], some_function_returning_string());
my problem is that i have tried so many different things that i am left confused. and have misunderstood the string array operations.
i have also tried with char menu_items[4][20]; and then strcpy(menu_items[0], "some text"); without any luck. and then there is the issue with how to make the function accept an array declared like this.
any suggestions on how to accomplish what i want would be very nice.
EDIT
i took some time reading the c programming book and found what i was looking for.
if i declare an array of pointers to strings as so char *menu_items[4] i will have an array that can take 4 pointers to strings or char arrays char *string
if i want to assign a string from a function returning a char * i have a function as so:
char *function_returning_string(int x){
static char *strings[3] = {"this","is","strings"};
if(x <= 2){
return strings[0];
}
else if(x > 2){
return string[1];
}
else{
return strings[2];
}
}
the code from where i call this function i have the following:
static char *other_strings = {"yes", "no"};
char *menu_item[3];
menu_item[0] = "ok";
menu_item[1] = other_strings[0];
menu_item[2] = function_returning_string(3);
then i can just pass the entire string array to functions that takes a *string_array[] as parameter as so function_takin_string_array(menu_item); or any string inside to functions taking char *string as parameter as so function_taking_string(menu_item[2]);
the code compiles without errors and works. if anybody think there is something wrong about the code or if have have misunderstood something please let me know.
As MFisherKDX noted this part of a code is incorrect:
static char *addr;
sprintf(addr, "S %d", i + 1);
return addr;
How do you think what are you returning here? It should be failed on sprintf() call because you try to assign some value to unallocated memory. It should be like this:
static char addr[128];
sprintf(addr, "S %d", i + 1);
return addr;
And how PeterJ_01 mentioned above try to use gdb to figure out what is wrong and where you have other mistakes.

LoadRunner - using a %d in lr_eval_string function

I need to create a string with multiple values of 'x'.
For instance I tried saving a string while referencing another variable as such:
lr_save_string("xyz", lr_eval_string("{x_%d}", intVar));
I've also have tried:
lr_save_string(lr_eval_string("{x_%d}", intVar), "xyz");
Is there any option/way to use %d and a int variable which always changes in the lr_eval_string function? or, how could this be performed?
You have to use sprintf function like below,
char *buffer = (char *)malloc(20); // allocate size as per your requirement
sprintf(buffer,"{x_%d}", intvar);
lr_save_string(lr_eval_string(buffer),"xyz");
Depending on the version of Loadrunner you may use the array functions:
lr_save_string(lr_paramarr_idx("x", intvar), "xyz");

Deleting a string inside of a struct in C

So, I'm trying to delete the content of a string inside of a struct.
i.e:
typedef struct data_player{
char name[25];
...
}player
player player_list[10]
And for example I want to delete the content of player_list[1].name
I tried player_list[1].name = "", but that didn't seem to work, any tips?
Thanks in advance.
To "erase" a C string it is sufficient to set its initial character to '\0':
player_list[1].name[0] = '\0';
The rest of the string content would remain there, but all standard functions working with C strings will safely ignore it.
Try this
memset(player_list[1].name, 0, sizeof(player_list[1].name));
This will set all the elements of array player_list[1].name to \0.
This will also work
strcpy(player_list[1].name, "\0");
Well, if you want to "delete" the content of a string you can put \0 like this:
player_list[1].name[0] = '\0';

Why tolower() affecting other string?

Why output of this program is always:
example
example
If i change first line with second in for loop, then output will look like this:
EXAMPLE
EXAMPLE
What i am doing wrong?
string key = "EXAmple";
string ukey = key;
string lkey = key;
for (int i = 0; i < strlen(key); i++)
{
ukey[i] = toupper(key[i]);
lkey[i] = tolower(key[i]);
}
printf("%s\n", ukey);
printf("%s\n", lkey);
The definition of string is likely to be char*. Consequently, key, ukey and lkey are actually pointers pointing to exactly the same memory; they are just aliases for the same thing.
Here, ukey and lkey are likely both pointers to the same array in memory. In C, an array reference is just a pointer to the first item in the array, and using the [] operator just returns the values at each position of the array (dereferenced).
So, ukey and lkey both refer to the exact same characters.
Sounds like you want to use strcpy() instead of ordinary assignment, or the equivalent for your custom type string.
Or use C++ and its string type.

C: How to store a string in a structure

So I have a structure, and one of its members is a string.
struct Output {
char *axis;
int value;
};
struct Output Jsoutput;
My question is, how do I store a string in axis?
char whichaxis[4][3] = {"LX","LY","RY","RX"};
// Store which axis and value of the joystick position in Jsoutput
Jsoutput.axis = whichaxis[jse.number];
printf("%s\n",Jsoutput.axis);
I feel like there should be some & somewhere, but not sure where.
Just use strdup
Jsoutput.axis = strdup(whichaxis[jse.number]);
You can copy a String with the function strcpy(destination, source)from string.h
see http://www.cplusplus.com/reference/cstring/strcpy/
Jsoutput.axis = malloc(3);
strcpy(Jsoutput.axis,whichaxis[jse.number]);
You don't have to "store" the string a second time.
char whichaxis[4][3] = {"LX","LY","RY","RX"};
Stores the string.
char *axis;
Says "I'm going to point at a string".
If you wanted a & in there, you could do:
Jsoutput.axis = & (whichaxis[jse.number][0]) ;
But the original designers of C were very pragmatic and let arrays turn into pointers all the time for convenience. See What is array decaying for more details.

Resources