c questions confused

djsfantasi

Joined Apr 11, 2010
9,160
I’m away from my computer, but I don’t think your program is doing what you think.

What is p? A pointer to the beginning of a memory block that you have allocated. And what is (p+1)? You have assumed that it is the address of the next student. You may be wrong. I think you’re addressing the memory block byte by byte, instead of int (RolNumber) by int.

It looks like it works, because you are incorrectly addressing memory consistently. Try running your sample program and entering 257, 258 and 259 for your student numbers and get back to me.
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
why code doesn't give output as it supposed to do

C:
#include <stdio.h>
 
int main()
{
   int n;
 
   printf("Enter an integer\n");
   scanf("%d",&n);
 
   printf("Value : %d\n", n);
  
   printf("byte is %d\n", sizeof(int));
 
   return 0;
}
When I enter 4294967296 I get 0
Enter an integer
4294967296
Value : 0
byte is 4

Enter an integer
4294967295
Value : -1
byte is 4

Enter an integer
125
Value : 125
byte is 4
 

djsfantasi

Joined Apr 11, 2010
9,160
why code doesn't give output as it supposed to do

C:
#include <stdio.h>

int main()
{
   int n;

   printf("Enter an integer\n");
   scanf("%d",&n);

   printf("Value : %d\n", n);
 
   printf("byte is %d\n", sizeof(int));

   return 0;
}
When I enter 4294967296 I get 0
Enter an integer
4294967296
Value : 0
byte is 4

Enter an integer
4294967295
Value : -1
byte is 4

Enter an integer
125
Value : 125
byte is 4
But your code IS giving the output it is supposed to!

There are limits when storing numbers in a computer system. Your code uses an int variable type. I assume it is on a 32 bit system.

in 32 bits, the largest number that can be stored is 2,147,483,647

When you enter 4,294,967,296, it can not fit into 32 bits. So it only uses those bits it can, which happens to represent 0.

it gets a little more complicated when you consider negative numbers; they use an extra bit to represent the sign and for various reasons, the bit pattern stored doesn’t represent the absolute value of the number. But first understand the limits on storing numbers in a computer system.
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
When we declare variable compiler will allocate memory location for variable.

C:
#include<stdio.h>

void loop (void)
{
    unsigned int i;
    for (i = 0; i < 5; i++)
    {
        printf("count :  %d \n", i );
        printf("address :  %d \n", &i);
    }
}
int main(void)
{   
    loop();

    return 0;
}
count : 0
address : 6422300
count : 1
address : 6422300
count : 2
address : 6422300
count : 3
address : 6422300
count : 4
address : 6422300

Compiler has allocated memory location 6422300

What happen about function loop () Does it store somewhere ? Where does it store ?
 

Analog Ground

Joined Apr 24, 2019
460
The function is put in memory just like variables. You can have a pointer to a function just like pointers to variables. The function pointer points to the first instruction to execute to start the function. A function pointer is specified the same as an array pointer. So, in your code experiment, try printing the pointer to loop( ).
 

jpanhalt

Joined Jan 18, 2008
11,087
Based on his other threads, I think the TS should concentrate on flashing an LED based on some timer, such as TIMER0. A peripatetic approach will get nowhere.
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Based on his other threads, I think the TS should concentrate on flashing an LED based on some timer, such as TIMER0. A peripatetic approach will get nowhere.
Yes that's in my list I do not have good understand of PIC16F877A. I am reading datasheet
The function is put in memory just like variables. You can have a pointer to a function just like pointers to variables. The function pointer points to the first instruction to execute to start the function. A function pointer is specified the same as an array pointer. So, in your code experiment, try printing the pointer to loop( ).
function pointer

C:
#include<stdio.h>

void loop (void)
{
    unsigned int i;
    for (i = 0; i < 5; i++)
    {
        printf("count :  %d \n", i );
        printf("address :  %d \n", &i);
    }
}
int main(void)
{   
    loop();
    void (*pointer)(void);
     pointer = &loop;
     printf("pointer :  %d \n", *pointer );
     printf("function address :  %p \n", pointer );
    return 0;
}
count : 0
address : 6422268
count : 1
address : 6422268
count : 2
address : 6422268
count : 3
address : 6422268
count : 4
address : 6422268
pointer : 4199520
function address : 00401460

so function would store at address 00401460 ?
 

MrSoftware

Joined Oct 29, 2013
2,196
Just call the function from your function pointer to check what you think:

C:
#include<stdio.h>

void loop (char* whoCalledMe)
{
    printf("Loop called by: %s\n", whoCalledMe);
}
int main(void)
{  
    loop("Call to loop()");
    void (*pointer)(char*);
     pointer = &loop;
     (*pointer)("Function pointer to loop()");

     printf("function address :  %p \n", pointer );
    return 0;
}
1573350576703.png
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Just call the function from your function pointer to check what you think:
can you tell me what happen when this line execute in program
printf("pointer : %d \n", *pointer );

I get this output
pointer : 4199520

I don't think that I was expecting
pointer store the address of another variable and we can get indirectly data by pointer dereferencing
 

djsfantasi

Joined Apr 11, 2010
9,160
can you tell me what happen when this line execute in program
printf("pointer : %d \n", *pointer );

I get this output
pointer : 4199520

I don't think that I was expecting
pointer store the address of another variable and we can get indirectly data by pointer dereferencing
To access the value that a pointer points to, you have to use the indirection operator *.

To print the pointer itself, just access the pointer variable with no operator.

And to get the address of the pointer variable, use the & operator.

What were you expecting? Without the rest of the program, we can’t tell if that should be what you were expecting.
 

MrSoftware

Joined Oct 29, 2013
2,196
It looks like C interprets function pointer names, and dereferenced names ("pointer" and "*pointer") the same. Using Microsoft Visual Studio Community 2019 (free and highly recommended) we can easily look at the memory. For this code:

C:
#include<stdio.h>

void loop(char* whoCalledMe)
{
    printf("Loop called by: %s\n", whoCalledMe);
}
int main(void)
{
    // loop("Call to loop()");
    void (*pointer)(char*);
    pointer = &loop;
    char myStr[] = "Function pointer to loop()";
    (*pointer)(myStr);

    printf("function address :  %p \n", pointer);
    printf("function address :  %p \n", *pointer);
    printf("function address :  %p \n", &loop);
    printf("address of pointer memory :  %p \n", &pointer);

    return 0;
}
I get this output:

1573403527139.png


And if we look at the memory allocated to the variable "pointer", that is look at the memory at address "&pointer", you can see the address stored that it points to. So at address 0x0135FE00 (address of "pointer"), you can see the value stored in "pointer" which is the address of "loop()", or 0x00941000:

1573403591510.png

Now if we go look in memory at what is stored at the address of loop() (0x00941000), you will see all of that push/pop stack stuff that happens during a function call that we talked about in the other thread:

1573403683062.png


These screen shots are from the free version of Microsoft Visual Studio Community 2019 (free), I highly recommend this as an IDE because the debugger and memory features are wonderful for learning. Also note that if you try to replicate this with default settings enabled (incremental linking enabled) you will get an extra jump in your function call. This is due to the edit-and-continue feature. To remove the extra jump just disable the edit and continue feature. Let us know if/when you run into that and we can explain it.
 
Last edited:

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
How to print each string in new line ?

C:
#include <stdio.h>
 
void Message( char *pointer)
{
    
    while (*pointer != '\0')
    {
        printf("%c", *pointer);
        
        pointer++;
    }
}
 
int main(void)
{
   char string1 [] = {"Hobby" }; 
   char string2 [] = {"Bobby" }; 
  
   Message(string1); // call function
   Message(string2); // call function
      
    return 0;
}
HobbyBobby
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
That's the purpose of "\n".
I know use \n use for new line. I wanted to make a function that can print the string data in new line

For example Why I would display data HobbyBobby in embedded system ?
I would be display as below
Hobby Bobby
or
Hobby
Bobby

My program is fail to display both the things
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Simply add
printf("\n");
Structure name is student and It has three members Name , Marks and strcut Student (Pointer of Structure).
S1, S2 and S3 are the structure variables
Code:
struct Student
{
    int Name[];
    int Marks;
    strcut Student *NextStudent;   /*Pointer of structure */
}S1, S2, S3;
C:
struct Student
{
    strcut Student *NextStudent;   /*Pointer of structure */
}S1, S2, S3;
What does it mean in the context of the structure. What does it store ? Does it store address of NextStudent
 

MrSoftware

Joined Oct 29, 2013
2,196
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.
 
Top