-2

I haven't been brushed up on my Data Structures and I am encountering a problem using structures.

I want to create a structure that will be pointers to values from an array I take from an input file.

Say for example I created a structure here:

struct complexnums {
    float * real;  //A ptr to real list
    float * imag;  //A ptr to imag list
};

int main()
{
    //Lets say this is an array I have taken from file input
    float real [] = {1.0, 2.0, 3.0, 4.0};
    float imag [] = {0.5, 1.0, 1.5, 2.0};

    //How can I assign the structure ptr's to these arrays?
    //Do I do it like this?
    complexnums complex = {&real[0],&imag[0]};
}

Given the example, above is that the correct way to assign the values to it? Will the struct actually get the pointers to those values above?

Also I was looking at a sample of how a struct look like and this person did this.

typedef struct {
    int sample;
    int *test1;
}
struct1, *struct2;

What is the difference between struct1 and struct2?

Sorry and let me know if this is understandable. If not I'll try to edit it the best I can.

NoobestPros
  • 65
  • 2
  • 8
  • 7
    Is there a reason for you to use C-style array ? Is there a reason to use raw pointers ? – Pierre-Antoine Guillaume Nov 21 '18 at 21:25
  • @PierreAntoineGuillaume is there a reason not to? – vandench Nov 21 '18 at 21:26
  • I would try to avoid assigning dynamic pointers to objects whose scope they don't belong in. This might work as the function you are creating the arrays is `main`, but if this were ANY other scope, the allocated array would become out of scope as soon as you leave that function/scope/block. Why not create it dynamically and free it when finished? – MPops Nov 21 '18 at 21:29
  • @PierreAntoineGuillaume the raw pointers will be used for divide and conquer algorithms later. As for C-style arrays, I am not sure what the difference is. I created my arrays like that as a sample – NoobestPros Nov 21 '18 at 21:29
  • 1
    Also, it's hard to answer this kind of question because you actually have 3/4 questions embedded into 1. – MPops Nov 21 '18 at 21:30
  • @MPops yes, I plan on later, when I use a divide and conquer method, to allocate memory for those arrays. I assume it's much better to allocate in main and assign it to the struct like that? – NoobestPros Nov 21 '18 at 21:30
  • 2
    @vandench I guess so. I think handeling references and stl objects makes a better learning for c++. Memory leaks are definitely an issue in numerous programs because it is a complex problem, linked to hard to understand problems like error handeling. So I'd advice std::vectors and smart pointers instead. – Pierre-Antoine Guillaume Nov 21 '18 at 21:30
  • @NoobestPros decide on a question to ask, then ask one at a time. Keep in mind that stack overflow is not designed to learn, but to find a peer-reviewed and state-of-art answer to a detailed question. Try to find some information on these concepts : "c++ references" "STL" "iterator". an exemple of the implementation would be in there : https://stackoverflow.com/a/28910332/4450986, but I don't know if that'd help you. – Pierre-Antoine Guillaume Nov 21 '18 at 21:39
  • 1
    Your edit just broke your question. It was fine before then. I was about to comment that yes: it is (was) fine, except overly complicated because you could have just used `= { real, imag }` since array types are elided to a pointer. – paddy Nov 21 '18 at 21:39
  • Please use containers and smart pointers in preference over C-style arrays and manual memory management. A naked `delete` is a *major* code smell in modern C++. – Jesper Juhl Nov 21 '18 at 21:45

2 Answers2

1

To answer the second part of your question, in the statement:

typedef struct myStruct {
    //...
} structType, *ptrToStructType;

The resulting type definitions are structType, being shorthand for struct myStruct, and ptrToStructType being short for structType*. Struct identifiers are optional (myStruct in this example, ommited in yours).

See: Confusion with typedef and pointers in C

Hal Jarrett
  • 825
  • 9
  • 19
1

To answer the first question, yes you can assign the pointers that way and is correct. But anybody reviewing the code could be confused. Thus renaming members and variables it can be a lot clearer.

In assigning complex by first defining it and then assigning the individual members it is clear that don't become mixed as it is easy with direct initialization to assign reals to imaginaries and vice versa.

struct complexnums {
    float * reals;  // A ptr to array of reals
    float * imags;  // A ptr to array of imaginarys
};

int main()
{
    float realArray [] = {1.0, 2.0, 3.0, 4.0};
    float imagArray [] = {0.5, 1.0, 1.5, 2.0};

    complexnums complex;
    complex.reals = realArray;
    complex.imags = imagArray;
}
Bo R
  • 2,334
  • 1
  • 9
  • 17