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';
Related
I would like to know to to move information from a array that looks like
i[0]=(
i[1]=9
i[2]=3
i[3]=5
i[4]=)
i[5]=3
.
.
.
in to another array to look like
j[0]=(935)3
I have tried and the only result I can get is a copy of the array with each character in a each element instead of all the character being in the first element.
code:
char test[100];
int n;
int k;
char i[16];
n=0;
char l[16];
FILE *infile;
infile=fopen(filename,"r");
while( fscanf(infile,"%s",test)!=EOF){
nums[n]=test;
sscanf(test,"%s",&l);
for(k=0;k<16;k++){
i[k]=l[k];
}
n++;
}
You may want to look into multi-dimensional arrays as well.
There is not STRING type variable in C, only an array of characters.
You can print them all together if you want with:
printf("%s\n",nameOfVariable);
But that is it, everything else you have to use characters.
Hope this helps you. :)
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
I need to make a function that takes a string and a character, the function needs to remove all the occurrences of the character in the string and returns the total number of characters removed. I have managed to modify the string as to be able to remove the unwanted character but I can't seem to be able to replace the old string with the new one. Thanks for any replies in advance.
This is what I've managed so far:
int clearstr(char *st,char u){
int i,j=0,total=0,size=strlen(st);
char *new_str=calloc(size,sizeof(char));
for(i=0;i<size;i++){
if(st[i]!=u){
new_str[j]=st[i];
j++;}
else total++;
}
new_str[j]='\0';
printf("%s",new_str);
//until here all is good ,new_str has the modified array that i want but i can't find a way to replace the string in st with the new string in new_str and send it back to the calling function (main),thanks for any help //
return total;
}
You create a new string but you are not using it yet. You can use a function like memcpy or strcpy to copy the contents. You also don't deallocate the memory of the calloc call; this creates a memory leak. Try something like:
...
new_str[j]='\0';
printf("%s",new_str);
strcpy(st, new_str); // Copy the contents of the new string to the original one
free(new_str); // Clear the memory of the allocation in this function, otherwise you get a memory leak
return total;
...
OK beginning C programmer here. What I'm attempting to do is create a function that populates a linked list from a text file. What I've done so far is to use fgets() and strtok() to iterate through the text file and I'm trying to load the tokenized strings into a function to populate the linked list. First off, when I use strtok, how do I capture the tokenized strings into char arrays or strings? So far, I've tried something like this:
char catID[ID_LEN+1];
char drinkType[1];
char itemName[MAX_NAME_LEN + 1];
while((fgets(line, sizeof(line), menufile)) != NULL) {
token = strtok(line, "|");
strcpy(data,strdup(token));
addCatNode(menu, catID);
printf("%s\n", catID);
i++;
while(token){
if(token)
{
strcpy(drinkType,strdup(token));
addNodeItem(&menu, drinkType);
strcpy(itemName,strdup(token));
addNodeItem(&menu, itemName);
token = strtok(NULL, "|");
}
}
}
but somehow I don't think that's the right approach. And of course when I try and load the data into the addNodeItem() function, whose prototype I've written like this:
void addNodeItem(BCSType* menu, char *nodeitem);
and try and add the item using this notation:
category->nodeitem
the compiler tells me that there is no member named 'nodeitem' in the struct. Of course there isn't, but I'm trying to load the name from the strtok() part, so how do I get the addNodeItem() function to recognize the name that I'm trying to pass into it? Very confused here.
The "category" struct in the linked list looks like this:
typedef struct category
{
char categoryID[ID_LEN + 1];
char categoryName[MAX_NAME_LEN + 1];
char drinkType; /* (H)ot or (C)old. */
char categoryDescription[MAX_DESC_LEN + 1];
CategoryTypePtr nextCategory;
ItemTypePtr headItem;
unsigned numItems;
} CategoryType;
There are several issues here, but for starters, strcpy(drinkType, strdup(token)) doesn't make a lot of sense.
token is a pointer to a portion of your input string, with the '|' separator replaced by a NULL.
strdup allocates strlen(token) worth of memory and copies the contents of token. So far so good. It returns the address of the new memory, which you don't store anywhere, so you can never free() it. This is a leak, and you could eventually run out of memory.
strcpy(drinkType,strdup(token)) copies that new memory into the memory pointed to by drinkType. Thats only 1 character. I don't know if that is big enough. Neither do you, since there could be anything in the file you are loading. This is a bug waiting to happen.
And then it seems like the addNodeItem() function is missing something. You are passing a value that represents one of the possible values in the structure, with no way of specifying which one. You would have better luck creating a local copy of CategoryType, assigning all the information from the tokenizer, and then copying the whole thing into a new node.
outline of algorithm:
while lines:
CategoryType newCat
tokenize line
copy tokens into correct members of newCat, using `strncpy` to ensure no overruns.
add newCat to linked List.
For the last step, you need to duplicate newCat before adding it, because it will be overwritten when you process the next line. You can either pass a copy to AddNodeItem, or have AddNodeItem make the copy. (There are other options, but these are probably most straightforward.)
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.