0

I init a Struct member like this below:

struct MyStruct {

  int member_a;

};
int main(){

MyStruct s;//method 1

MyStruct * ps;//method 2

return 0;
}

What's the difference between method 1 and 2 ??Why do someone use method 1 and some others use method2?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
yayaya
  • 135
  • 10
  • 1
    A [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) should explain it in detail. – chris Apr 26 '15 at 13:21
  • 7
    I think you'll need to study the basics again; there are rather too many misunderstandings for this question to be answerable. Your introductory book should tell you the difference between objects and pointers, and between struct members and local variables. Also, choose a language: C and C++ are very different. – Mike Seymour Apr 26 '15 at 13:22
  • 1
    First is a object of type "MyStruct". When you do MyStruct* ps you are creating a pointer to a object of type "MyStruct". I recommend you the same that @MikeSeymour – Shondeslitch Apr 26 '15 at 13:33
  • 1
    Agree with Mike Seymour's comment, except that the code as given is C++, not C. – Peter Apr 26 '15 at 13:49
  • @homepa, you have three answers, why don't you accept one? :) – gsamaras Apr 26 '15 at 14:57

3 Answers3

1

Your struct has one member, you do not add any other member later, you can't do that outside the struct.

See my example:

// Example 1
// Referencing a structure member locally in "main()" with the "dot operator"

#include <stdio.h>

struct Test // unique definition of the struct
{
    int x;
};

int main(void)
{
    struct Test sTest; // we create an instance of the struct

    sTest.x = 2;       // we assign a value to the member of the struct

    printf("x = %d\n",sTest.x);

    return 0;
}

So, when you do:

MyStruct s;//method 1

MyStruct * ps;//method 2

you actually do this:

MyStruct s;

you say to create a struct of type MyStruct, called s. Memory will be allocated for it, but its members are not manually initialized, which you might want to remember!

Then this

MyStruct * ps;

creates a pointer to your struct, called ps. That means, that ps is ready to point to a struct of type MyStruct. It is a POINTER to a struct, not a struct.

Source of my example is here.


As pointed by crhis, a book (see the relevant list of SO here) may be what you need, since there is much confusion in your post. An online tutorial would also be nice.

Also notice that C and C++ are two different programming languages.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Thanks for answering. In what situation use method 2 is better?? http://www.geeksforgeeks.org/write-a-function-to-reverse-the-nodes-of-a-linked-list/ This link shows method 2 to implement Linked List – yayaya Apr 27 '15 at 15:40
  • @homepa it depends on what you want to achieve. If you have to build a single linked list, then yes, pointers should be used, like in this example: https://gsamaras.wordpress.com/code/list-c/ – gsamaras Apr 27 '15 at 15:55
  • 1
    THanks explanation. The example is well understand – yayaya Apr 28 '15 at 16:01
0

You should use Method 1, because Method 2 doesn't declare a variable of type MyStruct, it declares a pointer (to a variable of type MyStruct).

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • In what situation I use method 2 is better?? – yayaya Apr 27 '15 at 15:42
  • Both are fundamentally different things. Method 1 declares a variable of type MyStruct while Method 2 declares a pointer. As you know, a variable holds its own data. On the other hand, a pointer does not hold data. It justs points to the address of another variable. In other words, a pointer can be used to address several different variables or memory locations (at different times). As user3340994 said, method 2 should be used whenever you're dealing with data structures. However, for simple uses, method 1 is better. –  Apr 28 '15 at 12:56
  • Thanks you explanation! method 2 is convenient if I want to modify values in the Struct – yayaya Apr 28 '15 at 15:59
  • "Method 2 is convenient if you use data structures viz. linked list, stack, queue, tree etc." struct is a keyword. It is not the same as data structure. struct is used to create user-defined data types. For example, you have created a new data type named 'MyStruct' in your program which can be used to declare variables. Here, MyStruct is a data type just like 'int','float','double' etc. –  Apr 28 '15 at 16:40
0

I generally use method_2. In data structures like binary trees, if I have a pointer to a struct_node, say temp_pointer, and now I need to change this to its left_child, I can simply make the pointer point to the left_child. Now, if I need to change some value in the left_child, I can simply change that value in the node to which temp_pointer points. This won't be possible with method_1. As there, we would have a separate copy of the left_child instead of a pointer to the left_child (a separate copy will have same values, but different address). method_1 will not change the value in the original node (i.e. the left_child), but only in the copy.

Also, suppose we have a mystruct_pointer and another temp_pointer. We can compare both (mystruct_pointer == temp_pointer), and check whether they point to the same node or not. This won't be possible with method_1.

Remember, this method_2 only declares a pointer to mystruct type. To actually create mystruct type, you will have to allocate memory using malloc or calloc.

Stuphan
  • 21
  • 7
nishantbhardwaj2002
  • 757
  • 2
  • 6
  • 18
  • I agree with your answer. In a specific situation like what u said : If I want to change value in the [ **Mystruct * s**] by method 2, that works. But it doesn't work by method 1. Am I correct ? – yayaya Apr 27 '15 at 15:48
  • Yes. Using pointer as in method1 you can modify the value of original object. Using a separate copy as in method2 you can only modify the new copy and not the original one. This is just a case where method1 is more useful than method2. On the other hand there maybe cases where method2 is a better solution. – nishantbhardwaj2002 Apr 27 '15 at 16:39