How to Change the Default Value of a Struct Attribute

Set default value for attribute of struct in C

Create a function to do it:

struct readyQueue* create_readyQueue()
{
struct readyQueue* ret = malloc( sizeof( struct readyQueue ) );
ret->CPU = -1;
// ...
return ret;
}

struct readyQueue* CPUchoose = create_readyQueue();

You have to remember to free the memory as well, so it may be better to pass in a pointer to an initialize function.

void init_readyQueue( struct readyQueue* q )
{
q->CPU = -1;
// ...
}

struct readyQueue* CPUchoose = malloc( sizeof( struct readyQueue ) );
init_readyQueue( CPUchoose );
// clearer that you are responsible for freeing the memory since you allocate it.

default value for struct member in C

Structure is a data type. You don't give values to a data type. You give values to instances/objects of data types.

So no this is not possible in C.

Instead you can write a function which does the initialization for structure instance.

Alternatively, You could do:

struct MyStruct_s 
{
int id;
} MyStruct_default = {3};

typedef struct MyStruct_s MyStruct;

And then always initialize your new instances as:

MyStruct mInstance = MyStruct_default;

How to set default values in Go structs

  1. Force a method to get the struct (the constructor way).

    From this post:

    A good design is to make your type unexported, but provide an exported constructor function like NewMyType() in which you can properly initialize your struct / type. Also return an interface type and not a concrete type, and the interface should contain everything others want to do with your value. And your concrete type must implement that interface of course.

    This can be done by simply making the type itself unexported. You can export the function NewSomething and even the fields Text and DefaultText, but just don't export the struct type something.

  2. Another way to customize it for you own module is by using a Config struct to set default values (Option 5 in the link). Not a good way though.

How to make a default value for the struct in C#?

You can't. Structures are always pre-zeroed, and there is no guarantee the constructor is ever called (e.g. new MyStruct[10]). If you need default values other than zero, you need to use a class. That's why you can't change the default constructor in the first place (until C# 6) - it never executes.

The closest you can get is by using Nullable fields, and interpreting them to have some default value if they are null through a property:

public struct MyStruct
{
int? myInt;

public int MyInt { get { return myInt ?? 42; } set { myInt = value; } }
}

myInt is still pre-zeroed, but you interpret the "zero" as your own default value (in this case, 42). Of course, this may be entirely unnecessary overhead :)

As for the Console.WriteLine, it simply calls the virtual ToString. You can change it to return it whatever you want.

How to attribute a struct member with a default value for xml serialization in c#

your answer is most probably in this post.

It explain how to create a boolean method that returns whether that element should be serialized.

Assign default value for struct field

This is not possible. The best you can do is use a constructor method:

type abc struct {
prop1 int
prop2 int // default value: 0
}

func New(prop1 int) abc {
return abc{
prop1: prop1,
prop2: someDefaultValue,
}
}

But also note that all values in Go automatically default to their zero value. The zero value for an int is already 0. So if the default value you want is literally 0, you already get that for free. You only need a constructor if you want some default value other than the zero value for a type.

Can't initialize a struct with a default value

Swift 5.1:

Variables, marked with var are optional in constructors and their default value will be used. Example:

struct Struct {
var param1: Int = 0
let param2: Bool
}

let s1 = Struct(param2: false) // param1 = 0, param2 = false
let s2 = Struct(param1: 1, param2: true) // param1 = 1, param2 = true

For older versions of Swift (< 5.1):

Default Initialization is all-or nothing for a Struct. Either you define defaults for all properties and you get an automatically generated initializer Struct1() plus an initializer with all of the properties, or you get only the initializer with all of the properties, including any that happen to have a set default value. You're going to have to implement a custom initializer.

struct Struct1 {
let myLet = "my let"
let myLet2: Bool
let myLet3: String

init(myLet2: Bool, myLet3: String) {
self.myLet2 = myLet2
self.myLet3 = myLet3
}
}

C - Default value for pointer within a struct

So it seems as though there is some inconsistancy in the default value for a pointer defined within a struct.

That's exactly the problem - the compiler is does not set an automatic (local) variable to any default. So you may get something that results in EXE_BAD_ACCESS, or you may get something that appears to work. It's luck of the draw (and is a bug even if it appears to work).

One of the drawbacks to C is that you are responsible for ensuring that your variables are initialized properly. A compiler may assist with warnings, not all compilers are as good as others in that regard.

is there a simple way to set a default within the structs definition?

There is not a way to do this in the definition of a struct. However, you can initialize a struct when a variable of that type is declared:

node first = { 99, NULL };


Related Topics



Leave a reply



Submit