473,416 Members | 1,660 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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

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
2 2309
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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
by: Alf P. Steinbach | last post by:
So, I got the itch to write something more... I apologize for not doing more on the attempted "Correct C++ Tutorial" earlier, but there were reasons. This is an UNFINISHED and RAW document,...
19
by: Christian Fowler | last post by:
I have a VERY LARGE pile of geographic data that I am importing into a database (db of choice is postgres, though may hop to oracle if necessary). The data is strictly hierarchical - each node has...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
59
by: Rico | last post by:
Hello, I have an application that I'm converting to Access 2003 and SQL Server 2005 Express. The application uses extensive use of DAO and the SEEK method on indexes. I'm having an issue when...
6
by: kelvlam | last post by:
Hello, I'm a new begininer with JavaScript. I'm trying to figure out which is the best approach, and to understand the differences between them. I have a <Aelement that's suppose to either...
5
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space...
169
by: JohnQ | last post by:
(The "C++ Grammer" thread in comp.lang.c++.moderated prompted this post). It would be more than a little bit nice if C++ was much "cleaner" (less complex) so that it wasn't a major world wide...
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.