By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

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

P: n/a
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;

}
Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Santa wrote:
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.

main()
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);

to the top of the file, right under #include <stdio.h>

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
mf*****@sbcglobal.net

Nov 13 '05 #2

P: n/a


Santa wrote:
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.

Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.