Can some "C" expert correct me an efficient way of manipulating the data from the pointer

S

Santa

Hello:

I am trying to manipulating the data from the pointer (the below
program) in the function "test1", Is there any best way of changing
the data from that pointer (I am changing by assigning the address
"&p2->x" to a poiner and then changing. Looklike this is not perfect,
can somebody correct me. My programn is below. Thanks,
==============================================================
#include <stdio.h>

typedef struct xyz
{
int x;
int y;
int z;
}xyz_t;

main()
{
xyz_t p1;

p1.x = 1;
p1.y = 2;
p1.z = 3;

printf("First time: %d %d %d\n",p1.x, p1.y, p1.z); //It should print 1
2 3
test1(&p1);
printf("Second time time: %d %d %d\n",p1.x, p1.y, p1.z); //It should
print 10 20 30

}

void
test1(xyz_t *p2)
{
int *p3;

p3 = &p2->x;
*p3 = (int)10;
p3 = &p2->y;
*p3 = (int)20;
p3 = &p2->z;
*p3 = (int)30;

}
 
M

Mark F. Haigh

Santa said:
Hello:

I am trying to manipulating the data from the pointer (the below
program) in the function "test1", Is there any best way of changing
the data from that pointer (I am changing by assigning the address
"&p2->x" to a poiner and then changing. Looklike this is not perfect,
can somebody correct me. My programn is below. Thanks,

First off, set your compiler up to generate as many warnings as is
reasonable. They're very helpful. Mine says:

foo.c:11: warning: return type defaults to `int'
foo.c: In function `main':
foo.c:19: warning: implicit declaration of function `test1'
foo.c:22: warning: control reaches end of non-void function
foo.c: At top level:
foo.c:26: warning: type mismatch with previous implicit declaration
foo.c:19: warning: previous implicit declaration of `test1'
foo.c:26: warning: `test1' was previously implicitly declared to return
`int'

Also, indent your source code if you expect anybody to read it.

==============================================================
#include <stdio.h>

typedef struct xyz
{
int x;
int y;
int z;
}xyz_t;

You probably should get the basics down before using typedefs for your
own structs. Skip it here.

foo.c:11: warning: return type defaults to `int'

int main(void)
{
xyz_t p1;

p1.x = 1;
p1.y = 2;
p1.z = 3;

printf("First time: %d %d %d\n",p1.x, p1.y, p1.z); //It should print 1
2 3
test1(&p1);

foo.c:19: warning: implicit declaration of function `test1'

You have used a function that you have not declared yet. Either move
the test1 function above main, or add

void test1(xyz_t *p2);

printf("Second time time: %d %d %d\n",p1.x, p1.y, p1.z); //It should
print 10 20 30

foo.c:22: warning: control reaches end of non-void function

return 0;
}

void
test1(xyz_t *p2)
{
int *p3;

p3 = &p2->x;
*p3 = (int)10;
p3 = &p2->y;
*p3 = (int)20;
p3 = &p2->z;
*p3 = (int)30;

None of these (int) casts are necessary.


Here's what you are looking for:


#include <stdio.h>

struct xyz
{
int x;
int y;
int z;
};

void test1(struct xyz *p)
{
p->x = 10;
p->y = 20;
p->z = 30;
}

int main(void)
{
struct xyz p1 = { 1, 2, 3 };

printf("First time: %d %d %d\n", p1.x, p1.y, p1.z);
test1(&p1);
printf("Second time: %d %d %d\n", p1.x, p1.y, p1.z);
return 0;
}


Mark F. Haigh
(e-mail address removed)
 
D

Debashish Chakravarty

Santa said:
Hello:

I am trying to manipulating the data from the pointer (the below
program) in the function "test1", Is there any best way of changing
the data from that pointer (I am changing by assigning the address
"&p2->x" to a poiner and then changing. Looklike this is not perfect,
can somebody correct me. My programn is below. Thanks,
==============================================================
#include <stdio.h>

typedef struct xyz
{
int x;
int y;
int z;
}xyz_t;

main()
{
xyz_t p1;

p1.x = 1;
p1.y = 2;
p1.z = 3;

printf("First time: %d %d %d\n",p1.x, p1.y, p1.z); //It should print 1
2 3
test1(&p1);


You have not declared the prototype of test1 before using it.

printf("Second time time: %d %d %d\n",p1.x, p1.y, p1.z); //It should
print 10 20 30

}

void
test1(xyz_t *p2)
{
int *p3;

p3 = &p2->x;
*p3 = (int)10;
p3 = &p2->y;
*p3 = (int)20;
p3 = &p2->z;
*p3 = (int)30;

}

You do not need p3.
All you need to do is:

p2->x=10;
p2->y=20;
p2->z=30;


The type of an decimal constant without a suffix is the first of the
following types in which its value can be represented int,long int,
long long int. The casts are not neccessary.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top