c questions confused

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
It's not clear what you're asking? The pointer inside your struct will point to nothing until you assign it a value. It's just a variable like any other variable.
I have added few steps in code but my question is same
What does "strcut Student *NextStudent; /*Pointer of structure */" store in structure ?

C:
#include<stdio.h>

struct Student
{
    int Name[];
    int Marks;
    strcut Student *NextStudent;   /*Pointer of structure */
};
{
    int main (void)
    {
        /*dynamic memory allocation for structure */
      struct Student  *NewStudent = (struct Student*) malloc(10 * sizeof(struct Student));
 
       if ( NewStudent  == NULL)
        {
           printf("Memory not Available \n");
           exit(1);
        }
       NewStudent -> Marks = 95;
       NewStudent -> NextStudent;
   
      free(NewStudent);
   return 0;
}
 
Last edited:

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
It looks like nothing, you haven't assigned anything to it. So it now holds whatever happened to be in memory when the struct was allocated.
I have declared another pointer of structure
C:
#include<stdio.h>
#include<stdlib.h>

struct Student
{
    int Marks;
    struct Student *NextStudent;   /*Pointer of structure */
};

    int main (void)
    {
        /*dynamic memory allocation for structure */
      struct Student  *NewStudent = (struct Student*) malloc(10 * sizeof(struct Student));
      struct Student  *NewStudent1 = (struct Student*) malloc(10 * sizeof(struct Student));

       if ( NewStudent  == NULL)
        {
           printf("Memory not Available \n");
           exit(1);
        }
       
       NewStudent -> Marks = 95;
       NewStudent -> NextStudent = NewStudent1;
      
       printf(" %d ", NewStudent -> Marks );
  
   return 0;
}
-> NewStudent -> NextStudent = NewStudent1;
First pointer of structure store the address of second pointer of structure ?
 
Last edited:

MrSoftware

Joined Oct 29, 2013
2,197
What you're trying to make is called a linked list, we just had a discussion about that here the other day, see if you can find that thread. There are tutorials all over the place for linked lists. Your general idea with NewStudent->NextStudent = NewStudent1 is correct, but you've allocated 10x more memory than you need to.
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
What you're trying to make is called a linked list, we just had a discussion about that here the other day, see if you can find that thread. There are tutorials all over the place for linked lists. Your general idea with NewStudent->NextStudent = NewStudent1 is correct, but you've allocated 10x more memory than you need to.
I have seen previous as well as other tutorials It's hard to understand basics.

I asked only one specific question

-> NewStudent -> NextStudent = NewStudent1;

Does First pointer of structure store the address of second pointer of structure ?
 

MrSoftware

Joined Oct 29, 2013
2,197
Print the addresses of the memory after malloc(), then print the value of NextStudent after you assign it and see what you get. If I just give you the answer you're not learning, but if you learn to check the answer yourself then you will be more successful in the future. :)
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Print the addresses of the memory after malloc(), then print the value of NextStudent after you assign it and see what you get.
C:
#include<stdio.h>
#include<stdlib.h>

struct Student
{
    int Marks;
    struct Student *NextStudent;   /*Pointer of structure */
};

    int main (void)
    {
        /*dynamic memory allocation for structure */
      struct Student  *NewStudent = (struct Student*) malloc(4 * sizeof(struct Student));
      
      //Print the addresses of the memory after malloc()//
      printf("address %p\n", sizeof(NewStudent));
      
      struct Student  *NewStudent1 = (struct Student*) malloc(4 * sizeof(struct Student));

       if ( NewStudent  == NULL)
        {
           printf("Memory not Available \n");
           exit(1);
        }
      
       NewStudent -> Marks = 95;
      
       NewStudent -> NextStudent = NewStudent1;
      
       //print the value of NextStudent after you assign it and see what you get.
       printf(" value : %d \n", NewStudent -> NextStudent );
      
       printf(" %d ", NewStudent -> Marks );
 
   return 0;
}
address 00000004
value : 11275776
95
I can't figure out above result
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Your printf statements are wrong.
I don't see what's the wrong with printf statements

C:
#include<stdio.h>
#include<stdlib.h>

struct Student
{
    int Marks;
    struct Student *NextStudent;   /*Pointer of structure */
};

    int main (void)
    {
        /*dynamic memory allocation for structure */
      struct Student  *NewStudent = (struct Student*) malloc(4 * sizeof(struct Student));
    
      //Print the addresses of the memory after malloc()//
      printf("address %p\n", NewStudent);
    
      //Print the addresses of the memory after malloc()//
      printf("address %d \n", &NewStudent);
    
      //Print the addresses of the memory after malloc()//
      printf("address %d \n", *NewStudent);
    
      struct Student  *NewStudent1 = (struct Student*) malloc(4 * sizeof(struct Student));

       if ( NewStudent  == NULL)
        {
           printf("Memory not Available \n");
           exit(1);
        }
    
       NewStudent -> Marks = 95;
    
       NewStudent -> NextStudent = NewStudent1;
    
       //print the value of NextStudent after you assign it and see what you get.
       printf(" value : %p \n", NewStudent -> NextStudent );
    
       printf(" value : %d \n", NewStudent -> NextStudent );
    
       printf(" value : %d \n", *(NewStudent -> NextStudent) );
   return 0;
}
address 00DA0DD8
address 6422312
address 14296728
value : 00DA0E00
value : 14290432
value : 14300832
 

djsfantasi

Joined Apr 11, 2010
9,163
I don't see what's the wrong with printf statements
Code:
      //Print the addresses of the memory after malloc()//
      printf("address %p\n", sizeof(NewStudent));
This is not an address. What could it be?

The rest of the code is so confusing, I’m not going to comment until you understand the correct response to this question of mine.

I’ve read your statement that you learn by experimenting. I sometimes take that approach as well. But, I only do so after demonstrating to myself that I understand the subject matter.

Earlier in the thread, I implored you to research linked lists. Now, I understand your refusal or inability to do so. Your knowledge of the basic concepts is weak due to your decision to learn by doing things wrong.
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Earlier in the thread, I implored you to research linked lists. Now, I understand your refusal or inability to do so. Your knowledge of the basic concepts is weak due to your decision to learn by doing things wrong.
I have read all the posts. You have not provided me any link and I am not asking questions about the linked list.

Please read post #85 and #86 I had one specific question I want to work on only the structure right now.
 
Last edited:

djsfantasi

Joined Apr 11, 2010
9,163
I have read all the posts. You have not provided me any link and I am not asking questions about the linked list.

Please read post #85 and #86 I had one specific question I want to work on only the structure right now.
Exactly to which I am referring, that one specific question. Sorry, but I’m going to limit my participation in this thread, as it is like trying to teach a pig to sing (famous quote; look it up). You may not feel like I had to apologize. That’s ok.
 

BobaMosfet

Joined Jul 1, 2009
2,113
I am confused by some keywords in C language .I am trying to understand their use in coding

Take a look at this code I am declaring variable x and assigning value
Code:
int x = 8
main()
{
   .......
}
I am declaring variable with extern keyword
Code:
 extern int x = 8
main()
{
   .......
}
anyone can help me understand. What does this exerern keyword do in the code ?
'extern' is a keyword to tell the compiler that it must look in other included files or libraries for the definition of that variable. Normally, variables in header files are 'extern' because that tells the compiler to look for the original definition elsewhere, and that you are NOT redeclaring or redefining that variable. Otherwise, it might think you are.
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Print the addresses of the memory after malloc(),
if I understand your question correctly you are asking to print the address of memory that malloc allocate
pointer holds address
Code:
printf("address %p\n", NewStudent);
you said this printf system are wrong. I though a lot and I was searching a lot

I looked into fallowing paragraph
The void pointer in C is a pointer which is not associated with any data types. It points to some data location in the storage means points to the address of variables. It is also called general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.
printf(" address : %p \n", (void*) NewStudent);
address : 00AD0DD8
malloc() returns a void pointer to the first allocated byte of memory.

Exactly to which I am referring, that one specific question. Sorry, but I’m going to limit my participation in this thread, as it is like trying to teach a pig to sing (famous quote; look it up). You may not feel like I had to apologize. That’s ok.
@djsfantasi i apologize for any inconvenience I appreciate your understanding and contribution

I am lacking somewhere in basics so I am going to back to address the problem
 

402DF855

Joined Feb 9, 2013
271
C:
struct Student  *NewStudent = (struct Student*) malloc(4 * sizeof(struct Student));
This allocates enough memory for 4 student records. Your semantics are wrong. You should treat "NewStudent" as an array. Here is how to create a circular linked list of 4 students.
C:
struct Student  *NewStudent = (struct Student*) malloc(4 * sizeof(struct Student));
NewStudent[0].NextStudent = &NewStudent[1]; // == NewStudent+1
NewStudent[1].NextStudent = &NewStudent[2]; // == NewStudent+2
NewStudent[2].NextStudent = &NewStudent[3]; // == NewStudent+3
NewStudent[3].NextStudent = &NewStudent[0]; // == NewStudent+0
 

MrSoftware

Joined Oct 29, 2013
2,197
The incorrect printf looked like the intent was to print an address, but it was printing the result of sizeof(), I don't see it now so maybe you edited it.
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Print the addresses of the memory after malloc(),
@MrSoftwareperhaps You asked me difficult questions. Can you please point of your post 86
A void pointer can hold address of any type
Code:
      printf(" address of NewStudent : %p \n", (void*)&NewStudent);
      printf(" address NewStudent1: %p \n", (void*)&NewStudent1);
 

MrSoftware

Joined Oct 29, 2013
2,197
Your print's above should work, though you don't need the cast to void*. Generally speaking, try not to cast to void* unless you really need it. Type checking is a feature of the compiler that helps prevent people from making mistakes. When you cast to void*, you're telling the compiler not to check the type and trust me it's OK. This makes it a lot easier to introduce bugs. In this case with your print statements it will work fine, but it's not needed so it's better to leave it out so you don't get in the habit of using it.
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
I have defined a = 200 locally (within the int main() function) and global a = 100

Code:
#include<stdio.h>

int a = 100; // global variable a = 100

int main() {

  int a = 200;   // local variable a = 200

  printf(" Value assign to a : %d \n", a);  

  printf("Address of variable a : %p \n", &a);

  return 0;
}
Program Output
Value assign to a : 200
Address of variable a : 0061FF2C

Compiler allocate only one memory location 0061FF2C to store the value of a.

What happen when this program will execute on computer?
 

bogosort

Joined Sep 24, 2011
696
Compiler allocate only one memory location 0061FF2C to store the value of a.
You're correct that the compiler allocates memory for only one of the 'a' variables, but it's not the one you think.

The first declaration of 'a' lies outside of any function and so it has static duration (lives for the entirety of the program). Since the variable wasn't declared with the 'static' keyword, it has external linkage (can be seen by any other source files in the program). In other words, the first 'a' is a "global" variable, and the compiler assigns its value (100) to the DATA section of the executable.

The second declaration of 'a' occurs within the main() function, which is a block of code, and so it has no linkage (cannot be seen outside the function block). Since it was declared without the 'static' keyword, it has automatic duration (lives for the life of the function block). As it is an automatic variable, the compiler does not allocate memory for it -- at run time, its value will be placed on the stack.

The problem, of course, is that you've effectively hidden the global identifier 'a' with the local version. Since the local version won't go out of scope until main() returns -- which is effectively the lifetime of the program -- the global 'a' is unusable and orphaned. If you actually want two distinct identifiers, rename one of the them. On the other hand, if you want only a single variable, you have two options: (1) remove the re-declaration in main(), so that "int a = 200;" becomes "a = 200;". Or, (2) declare the second 'a' as an extern variable:
C:
int a = 100; // global
int main(void) {
  extern int a; // same global variable as above
  a = 200;
  //...
}
The extern keyword tells the compiler that this 'a' refers to a variable of the same name that was defined outside the block of code. You don't see this construct too often -- option (1) above is far more common -- but it has its place.
 
Top