May I Create an Instance of a Structure Using a Simple Int

May I create an instance of a structure using a simple Int?

You can make that work by adopting the IntegerLiteralConvertible
protocol:

extension Barometer: IntegerLiteralConvertible {
public init(integerLiteral value: Int) {
self.init(pressure: value)
}
}

Now a Barometer value can be instantiated from a literal integer:

let barometer: Barometer = 80
print(barometer) // Barometer(pressure: 80)

But note that this works only with literals, not with arbitrary
integer values:

let p = 80
let barometer: Barometer = p
// error: cannot convert value of type 'Int' to specified type 'Barometer'

// You have to use:
let barometer = Barometer(pressure: p)

For Swift 3, replace IntegerLiteralConvertible by
ExpressibleByIntegerLiteral.

How to create a new instance of a struct

The second way only works if you used

typedef struct listitem listitem;

before any declaration of a variable with type listitem. You can also just statically allocate the structure rather than dynamically allocating it:

struct listitem newItem;

The way you've demonstrated is like doing the following for every int you want to create:

int *myInt = malloc(sizeof(int));

How to create an instance of a struct in C++?

You're assigning bk to NULL and then trying to access a member of it. That's the same as a null pointer in Java, and what you're doing would typically raise a NullPointerException (thanks from the comments). If you want to create a pointer to your struct, you need to use operator new:

bk = new _book;
// ...
return bk;

and then make sure you call delete on the pointer when you're done with it.

I would advise against using pointers here, though. Unlike other languages, C++ lets you create objects by value. It also allows for references and pointers, but only use pointers when you absolutely must. If you simply want to create a book object with a given pageNum, you should create a constructor while you're at it:

struct _book {
int pageNum;
_book(int n) : pageNum(n) // Create an object of type _book.
{
}
};

and then you can invoke it like

_book myBook(5); // myBook.pageNum == 5

If you're new to C++, please get yourself a good book on it. It's not just a low-level language, and it's also not just an OOP language. It's a multi-paradigm swiss army knife language.

How do I create a new instance of a Structure in F#?

You use the new keyword and define a constructor for it.

For example:

type simple = 
struct
val A : int
val B : int
new (a: int, b: int) = { A = a; B = b; }
end

let s = new simple(1, 2)

Can I have a Structure as member of its own structure in C?

To begin with, struct type; is a forward declaration, not a member variable definition. Secondly, no you can't do it, you need to use pointers.

To use a structure it must be fully defined, the definition have to end (with the closing }) before it can be used. Otherwise the compiler doesn't know the full size of the structure and don't know how big it is and how must memory to reserve for instances of the structure.

But to create a pointer to a structure, all the compiler needs to know is the structure tag (type in your case). That's because the size of a pointer is know at compile-time. The size of the full structure itself doesn't need to be known at that point.

Creating a certain struct at run time in c

a variable have 3 fields:
1) type, 2) name, 3) address.
you shuold create an array of struct containing these 3, array of this struct will be what you want

your structs may look like this:

typedef enum _Type{T_INT,T_STRING}Type;
typedef struct _var{
Type type;
char* name;
union {int n; char* str;} data;
}var;

typedef struct _Struct{
int count;
var* array;
} Struct;

when you get the input, you need to build the Struct according to it.
name : char[50], address: char[50] and age: int

Struct *s = malloc(sizeof(Struct));
s->count = 3;//count of fields in the input
s->array = malloc(s->count*sizeof(var));
//you really should do it in a loop, after parsed the input...
for(i=0;i<s->count;i++){
s->array[i].name = strdup(parsedname);//"name", "address", "age"
s->array[i].type = strcmp(parsedtype,"int")?T_STRING: T_INT;
//for string you need to alloc memory for string...
if(s->array[i].type == T_STRING)
s->array[i].data.str=malloc(50 /*the size you've got*/);
//not need to alloc memory for the int
}

when you finish don't forget to free the mallocs:

for(i=0;i<s->count;i++){
free(s-array[i].name);
if(s->array[i].type == T_STRING)
free(s->array[1].data.str);
}
free(s->array);
free(s);

You'll also need a method to fill the struct and print it, and so on...

How do you create a new instance of a struct from its type at run time in Go?

In order to do that you need reflect.

package main

import (
"fmt"
"reflect"
)

func main() {
// one way is to have a value of the type you want already
a := 1
// reflect.New works kind of like the built-in function new
// We'll get a reflected pointer to a new int value
intPtr := reflect.New(reflect.TypeOf(a))
// Just to prove it
b := intPtr.Elem().Interface().(int)
// Prints 0
fmt.Println(b)

// We can also use reflect.New without having a value of the type
var nilInt *int
intType := reflect.TypeOf(nilInt).Elem()
intPtr2 := reflect.New(intType)
// Same as above
c := intPtr2.Elem().Interface().(int)
// Prints 0 again
fmt.Println(c)
}

You can do the same thing with a struct type instead of an int. Or anything else, really. Just be sure to know the distinction between new and make when it comes to map and slice types.



Related Topics



Leave a reply



Submit