Assistance with error message required - c

I have an assignment. I have the entire code but the snippet I am stuck on is the following.
The user must choose between the following projectiles.
Each projectile has a blast radius and this is the code I have so far (for this section):
userProjectileChoice = myProjectiles[3];
myProjectiles[0].projectileName = "cannonBall";
myProjectiles[0].blastRadius = 10;
myProjectiles[1].projectileName = "highExplosiveShell";
myProjectiles[1].blastRadius = 1;
myProjectiles[2].projectileName = "mortarBomb";
myProjectiles[2].blastRadius = 1000;
I need to use the struct as it is required.
Error message is: incompatible types when assigning to type int from type struct
The following has been included so you can see what the above code is referring to.
Projectile name has not been declared, am I declaring it in the above coding?
struct Projectile
{
char name[40];
float blastRadius;
};
float blastRadius;
double cannonBall = 0;
double highExplosiveShell = 1;
double mortarBomb = 2;
struct Projectile myProjectiles[3] = {{"cannonBall",0},{"highExplosiveShell", 1},{"mortarBomb", 2}};
Any assistance would be appreciated, but please keep it simple for me to understand as I said before I am new to this.

I'm not sure what type userProjectileChoice is, so I'm going to assume that it is the same type of the myProjectiles array. Try assigning each property one at a time as so:
userProjectileChoice.projectName = myProjectiles[3].projectileName
userProjectileChoice.blastRadius= myProjectiles[3].blastRadius
It's been years since I've programmed in C, but it's worth a shot.
Or perhaps you mean to assign the "index" and not the entire struct. Like I said, it's difficult to come up with a correct answer when I don't know the type of userProjectileChoice. Perhaps your problem is in the assignment at the beginning of the code. So if the 4th choice is selected then it should be as so:
userProjectileChoice = 3

Related

Remapping C pointer to a 3D array

I am trying to get a pointer to an array integers to be temporarily remapped in a function later on to save myself pointer math. I've tried to see if any other questions answered it, but I've been unable to reproduce the methods described here, here, and here.
Fundamentally, I just want to temporally treat an integer group and a 3D array to be sure that I don't mess up the pointer math. (I'm looking at this currently because the previous code had made inconsistent assignments to the memory).
#include <stdlib.h>
#define GROUPCOUNT 16
#define SENSORCOUNT 6
#define SENSORDIM 3
int main()
{
int *groupdata = (int *)calloc(GROUPCOUNT * SENSORCOUNT * SENSORDIM,sizeof(int));
int sensordata[SENSORCOUNT*SENSORDIM];
sensordata[7] = 42; //assign some data
int (*group3d)[GROUPCOUNT][SENSORCOUNT][SENSORDIM] = groupdata; //<---Here is the problem
group3d[1][5][1] = sensordata[7]; //I want to do this
free(groupdata);
}
In the example above, I want to handle groupdata as group3d temporarily for assignments, and I just cannot seem to wrap myself around the casting. I currently have macros that do the pointer math to enforce the correct structure, but if it was all just in the code, it would be even better when I pass it off. Any suggestions would be greatly appreciated.
note: The 3D cast is to be used in a function way in the bowels of the program. The example is just a minimally viable program for me to try to sort out the code.
When group3d is defined with int (*group3d)[GROUPCOUNT][SENSORCOUNT][SENSORDIM], then *group3d is a three-dimensional array. That would let you use it with (*group3d)[1][5][1].
To use it with group3d[1][5][1], you need group3d to be a pointer to a two-dimensional array:
int (*group3d)[SENSORCOUNT][SENSORDIM] = (int (*)[SENSORCOUNT][SENSORDIM]) groupdata;
(There are some technical concerns about C semantics in aliasing an array of int as an array of array of array of int, but this is not a problem in common compilers with default settings. However, it would be preferable to always use the memory as an array of array of array of int, not as an array of int.)
int l = 5, w = 10, h = 15;
int Data = 45;
int *k = malloc(l * w * h * sizeof *k);
// not use this k[a][b][c] = Data;
//use this is right
k[a*l*w + b*l + c] = Data;
In the example above, I want to handle groupdata as group3d
temporarily for assignments, and I just cannot seem to wrap myself
around the casting.
One possible solution is to create a multidimensional array dynamically like this. This way you won't have to cast things or worry about the dimensions.
int (*group3d)[GROUPCOUNT][SENSORCOUNT][SENSORDIM] = calloc(1, sizeof(int [GROUPCOUNT][SENSORCOUNT][SENSORDIM]));
(*group3d)[1][5][1] = sensordata[7]; //I want to do this
/* Then you can print it like */
printf("%d\r\n", (*group3d)[1][5][1]);

C Programming - Non static initialization of a flexible array member

I think my english is just to bad to understand the other articles about this. But anyway:
I just thought i could write a program (in C), that can store a set of cards.
Not complicated, just store values and names of cards and print them out.
I'm a beginner in C, and because i'm in the section "Strings in Structures" in my Book, i wanted to try out structures on my own. This is my Code so far:
#include <stdio.h>
struct card
{
int value;
char name[];
};
int main(void)
{
const struct card heart[13] = { {2,"two"}, {3,"three"}, {4,"four"}, {5,"five"}, {6,"six"}, {7,"seven"}, {8,"eight"}, {9,"nine"}, {10,"ten"}, {11,"jack"}, {12,"queen"}, {13,"king"}, {14,"ace"} };
int i;
for (i = 0; i < 13; ++i)
{
printf("The card heart-%s has the value of %i", heart[i].name, heart[i].value);
}
return 0;
}
I just wanted to test if it works, so i just wrote the heart-cards in the code. If i want to compile this file, my compiler (gcc/mingw) hits me with 26 errors. It says:
"(near initialization of heart[0])"
"non static initialization of a flexible array member"
I don't really understand this. In the book, everything works as expected. I tried to rebuild the code in the book and changing the names, but it doesn't work. I think it's a problem with the strings, because if i use integers only, everything works.
In already read in another post, that every string should be allocated manually, and there was a code example, but i don't know what all the lines should mean, and i want understand what my code does, so i don't copy + paste.
Could you explain me why this doesn't work?
PS: I am writing currently in windows, so please don't use bash commands to explain or something like that.
I am also german and my english is not the "yellow of the egg", try to explain without using complex 'sentence builds' (i hope you know what i mean :D) and unusual words.
Thanks for all help!
You need to create some space for the name of each card. Easiest way to do this would be to change your struct card definition to something like:
struct card
{
int value;
char name[16]; // doesn't have to be 16, but make sure it's large enough to hold each card name plus a '\0' terminator
};
The prior answers suggest allocating a fixed length for your names. This has limitations and even dangers. It is always a good idea to avoid it all together.
e.g. You want to alter the name during the game, e.g. "Ace (Trump Card)" but that might be both too long even worse overwrite memory. (Many of the known vulnarabilities in code are caused by buffer overruns)
You are also building in a limitation; What if your game needs translating into another language?
By using pointers, you don't need to resort to either variable length structures or fixed string lengths.
You also add the ability to add API access functions that set data, allowing checks before it's written, preventing buffer overruns.
Instead of using character array (aka strings) you should use pointers in your structures. If you follow the link at the bottom I take this further and use pointers to the structures themselves.
As the pointer storage size never changes your names can be of any length and even altered later, perhaps as the game progresses.
Your card could look something like
typedef struct card
{
int value;
char * name;
}
Now the initial assignment can be done like this
card_t card_ace = {14, "Ace"};
And the values are not fixed (unless that is what you want, then you make them const).
card_ace.value = 200;
card_ace.name = "Trump card";
or an array of cards like this
card_t suit_hearts[] = {{2,"two"}, {3,"three"}, {4,"four"}, {5,"five"}, {6,"six"}, {7,"seven"}, {8,"eight"}, {9,"nine"}, {10,"ten"}, {11,"jack"}, {12,"queen"}, {13,"king"}, {14,"ace"}}
Even better make the whole thing using pointers
typedef card_t * cards_t;
cards_t mysuit = &(card_t){2,"two"}, &(card_t){3,"three"}, ...
Perhaps consider makeing the suit a structure.
typedef struct
{
char * name;
card_t ** cards;
} suit_t;
typedef card_t * cards_t[];
suit_t mysuit = {
.name = "Hearts",
.cards = (cards_t){&(card_t){2,"two"}, &(card_t){3,"three"},....}
}
* For a fully working example of the latter, demonstrating using arrays of pointers to sidestep the limitations of variable length members of fixed arrays, see this gist on github

Dynamic array of structure in C

I am trying to create a dynamic array of structures using malloc(). I have looked at some other answers already on this forum, but I got lost in answers that weren't the same from one question to the next. Here's what I did so far:
//This is above main()
struct linesegs
{
float*X;
float*Y;
float*Z;
float*Q;
float*R;
float*S;
};
struct linesegs *LINE;
//Inside main() I have this:
LINE = malloc((facets*3)*sizeof(linesegs));
//Later on I access the struct like so:
LINE[variable].X = SomeFloatNumber
LINE[variable].Y = SomeFloatNumber
LINE[variable].Z = SomeFloatNumber
LINE[variable].Q = SomeFloatNumber
LINE[variable].R = SomeFloatNumber
LINE[variable].S = SomeFloatNumber
From what I have read the above should be very close to correct, but my compliler underlined the = in LINE = malloc((facets*3)*sizeof(linesegs)); and gives the following error:
A value of type "void*" cannot be assigned to an entity of type "linesegs"
The places I learned this from were not exactly clear what goes where, and in what order they should go. I tried moving stuff around and playing with the pointers to no avail. Hopefully this question will help others (and me) by providing one solid answer with no confusion =). Thanks in advance for help with this.
would this solve the error or warning?
LINE = (struct linesegs *)malloc((facets*3)*sizeof(struct linesegs));

Copy C Struct with char pointer

Firstly, I am really sorry if this has already been asked and resolved - I have spent ages searching and trying to adapt code samples to give me what I need... but sadly to no avail. Essentially I am just trying to copy the contents of one struct to another (which is documented here elsewhere but I cannot get it to work).
A scanner populates the following struct when it reads a barcode:
struct barcode
{
char *text;
int length;
int id;
int min;
int max;
};
This is instantiated as:
static struct barcode code = {0};
I instantiate another one of the same type:
struct barcode *barcodeHolder;
This is intended to store a copy of the scanned barcode. This is because other codes will then be scanned that indicated other steps such as barcodes to indicate numbers or stages (eg. end, start, etc). Once I want to write the struct contents to disk I use the "copy" of the struct as that is what I want.
However, the char *text property always equals 'c' and not the value of the barcode.
I copy them as follows:
barcodeHolder = malloc(sizeof(code));
barcodeHolder->text = malloc(strlen(code->text) + 1);
strcpy(barcodeHolder->text, code->text);
barcodeHolder->id = code->id;
barcodeHolder->length = code->length;
barcodeHolder->max = code->max;
barcodeHolder->min = code->min;
This is what I have got from other posts on a similar topic.
However, I am clearly doing something stupidly wrong and would welcome any help anyone might be able to offer so that my copy of the struct text element does actually get the right value copied.
Thank you!
Your code is not a pointer
You need this:
barcodeHolder = malloc(sizeof(code));
barcodeHolder->text = malloc(strlen(code.text) + 1);
strcpy(barcodeHolder->text, code.text);
barcodeHolder->id = code.id;
barcodeHolder->length = code.length;
barcodeHolder->max = code.max;
barcodeHolder->min = code.min;
I don't believe that your code is really:
static struct barcode code = {0};
[..]
strcpy(barcodeHolder->text, code->text);
Because the last statement would yield a compile error - because code is not a pointer you have to use code.text there (instead of code->text).
Assuming that you are actually using something like
struct barcode *code = ...;
You are allocating with your above code sizeof pointer of struct code bytes which is not enough for your structure.
Thus, copy it like this:
barcodeHolder = malloc(sizeof(struct barcode));
// alternative: ... = malloc(sizeof(*code));
*barcodeHolder = *code;
barcodeHolder->text = malloc(strlen(code->text) + 1);
strcpy(barcodeHolder->text, code->text);
Or, more simply:
barcodeHolder = malloc(sizeof(code));
*barcodeHolder = code;
barcodeHolder->text = strdup(code.text);
Probably unrelated, but if code in your example is a pointer to a struct, then your sizeof is wrong. It should be sizeof(*code) or sizeof(struct barcode). – Sean Bright 21 hours ago
#SeanBright In no way would I ever call myself a C programmer - ever! - so you saved completely here! Thank you. No, "code" is not a pointer in programme which confused me too... and was probably why I couldn't get anywhere. I just needed to get it work to prove we can do it for a demo. Should it progress I will come back (hopefully!) and revisit the code to better understand why it is working when it maybe shouldn't be. Thank you again!!!

Referring to data both by name and by number in C

I have twenty or so integers which I want to be able to refer to by name when they're being set, but I would like to also be able refer to them by number like they were in an array, so I can print them out one by one using a for loop. Any ideas how to code this in C? Here's what I'm talking about in pseudo code:
/* a data structure to keep a count of each make of car I own */
my_cars;
/* set the counts */
my_cars.saabs = 2;
my_cars.hondas = 3;
my_cars.porsches = 0;
/* print the counts */
for(all i in my_cars) {
print my_cars[i];
}
Is this asking too much of a low level language like C?
struct car {
const char *name;
int count;
} my_cars[] = {{"Saab", 2}, {"Honda", 3}, {"Porsche", 0}};
int i;
for (i = 0; i < sizeof(my_cars) / sizeof(my_cars[0]); i++)
printf("%s: %d\n", my_cars[i].name, my_cars[i].count);
To do that you should use an array instead of standalone data fields
#include <stdio.h>
typedef enum CarType {
CART_SAAB,
CART_HONDA,
CART_PORSHE,
CART_COUNT_
} CarType;
typedef struct MyCars {
unsigned ncars[CART_COUNT_];
} MyCars;
int main(void)
{
MyCars my_cars = { 0 } ;
unsigned i;
my_cars.ncars[CART_SAAB] = 2;
my_cars.ncars[CART_HONDA] = 3;
for (i = 0; i < CART_COUNT_; ++i)
printf("%u\n", my_cars.ncars[i]);
return 0;
}
C can do anything any other language can do. This does look like homework and I bet you are expected to make something with a key. Remember, your instructor wants you to use the data structures he or she is trying to teach you. He doesn't really want the problem solved in any random way, he wants it solved applying the topics you have been discussing.
So think about a data structure containing both strings and counts, one that can be searched, and provide functions to do that. What you are likely to get here are nice, professional, simple solutions to the problem. And that's not really what your instructor wants...
enum Makes { SAAB, HONDA, PORSCHE, INVALID };
int my_cars[INVALID];
my_cars[SAAB] = 2;
my_cars[HONDAS] = 3;
my_cars[PORSCHE] = 0;
You need two data structures. An array to hold the numbers, and a map from the name to the index in the array. In C++ you'd use one of the map classes in the standard library. I don't know what's available in C but I'm sure there are map implementations available.
The low-level C way to do this would be to wrap the cars structure into a union:
// define a structure for the cars.
typedef struct
{
int saabs;
int hondas;
int porsches;
} cars;
// wrap it into a union:
typedef union
{
cars byname;
int byid[3]; // Note: Number and type must match with the cars structure.
} cars2;
int main (int argc, char **arg)
{
cars2 my_cars;
int i;
// fill out by name:
my_cars.byname.saabs = 1;
my_cars.byname.hondas = 5;
my_cars.byname.porsches = 3;
// print by index:
for (i=0; i<3; i++)
printf ("%d\n", my_cars.byid[i]);
}
Umm...based on what you've pseudo coded up there you could probably use a union. The answers others are giving seem oriented around allowing a mapping between names and numbers. If thats what you're looking for (as in, being able to print the names) then their answers will be better. However it sounds like to me you're simply looking for clarity in the code to allow you to reference things by name or number, in this case a union would be ideal I think. This is exactly the type of thing a low level language like C lets you do.
union my_cars {
struct names {
int saab;
int ford;
...
}
int[NUM_MAKES] nums;
}
You will have to be careful to ensure NUM_MAKES is in sync with the number of makes you define. Then you can do something like:
my_cars.names.saab = 20;
my_cars.nums[0] = 30;
And modify the same element.
FYI, my C is a little rusty so there may be syntax errors there, feel free to correct.
EDIT:
Ahh, I read some of the other answers using ENUMs or DEFINEs and those might actually be simpler/easier than this one...but unions are still worth knowing.
There are maybe a couple of options.
It is possible to have the same space in memory defined (and used) in two different ways. In other words, you could have a struct with the named members and reference it either as the struct or as an array depending on how you intended to address it.
Alternatively, you could do an enumerated typedef that names the locations in the array, e.g.
typedef enum {
SABS = 0,
HONDAS,
PORSCHES
} cars;
This would then allow you to refer to offsets in the array by name, e.g.
mycars[SABS] = 5;

Resources