LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-25-2006, 08:11 AM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Rep: Reputation: 30
C: structs and pointers question


Hi,
how can i get the print function to print the the argv[i] instead from their addresses? I tried changes in the print function, like "r.p1.(*x)", "r.p1.*x" and "*r.p1.x", which didn't work...
Code:
#include <stdio.h>

struct point {
	int* x;
	int* y;
} a_point, b_point;

struct rect {
	struct point p1;
	struct point p2;	
} a_rect;

void print(struct rect r)
{
	printf("%i || %i \n", r.p1.x, r.p1.y);
	printf("%i || %i \n", r.p2.x, r.p2.y);	
}

main(int arc, char *argv[])
{
	
	a_point;
	a_point.x=argv[1];
	a_point.y=argv[2];
	
	b_point;
	b_point.x=argv[3];
	b_point.y=argv[4];
		
	a_rect.p1=a_point;
	a_rect.p2=b_point;
	print(a_rect);
	
}
 
Old 03-25-2006, 08:58 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
argv is a character array, so you will need to convert it from the string to an integer before assigning it. look at the atoi function for some assistance
 
Old 03-25-2006, 08:33 PM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
As an aside.. (assuming your on linux) I'd recommend strtol or strtoll over atoi....

long strtol (char *str, char *endptr, int base);
long long strtoll (char *str, char *endptr, int base);

base can be any number from 2 to 36.. endptr is a pointer to the place in the string you want it to stop, just pass in null if you want it to stop either at the first invalid char or the end of the string.

Example:
a_point.x = strtol(argv[1],NULL,10);
 
Old 03-27-2006, 05:54 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
The * goes at the front: '*r.p1.x'. 'r.p1.*x' means that some variable 'x' in the current scope is a pointer to either a member function or member data, whereas 'r.p1.x' is the fully-qualified pointer that you need to dereference.

In your particular case, however, you are trying to convert members of the 'argv' array to integers, however they are taken off the command line as strings. You are looking for something like 12345, whereas the data you are looking at is "12345" (an array of 5 characters.) You need to convert them to integers, which can be done with atoi.
ta0kira
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
question about pointers vijeesh_ep Programming 1 09-13-2004 03:13 PM
a question about array pointers in C veritas Programming 12 07-18-2004 06:31 PM
pointers to structs in C spuzzzzzzz Programming 5 06-03-2004 05:41 PM
question on pointers h/w Programming 1 10-26-2003 02:54 PM
question on pointers to functions h/w Programming 3 10-06-2003 04:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration