LinuxQuestions.org
Help answer threads with 0 replies.
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 05-14-2004, 11:15 PM   #1
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
data type to hold pointers?


this is probably a stupid questions but what data type is used for pointers?

what i want to do is set up a structure that (at one of its members) will hold various pointers to other structures, which will also hold pointers to lines of text, taken from getline() (using dynamic allocation for the memory thing)

also does anyone know what size is for dynamic allocation?, getline wants me to input the size of the buffer, but since its supposed to change itself I'm not sure if there is a size..?, or should i just use sizeof() to get the size it asks for?

- thx
 
Old 05-14-2004, 11:21 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You can use void * to hold a typeless pointer. For example:

Code:
struct s1 { int a; };
struct s2 { char a; };
struct s3 { void *sptr; };

int main(void)
{
  struct s1 str1;
  struct s2 str2;
  struct s3 str3, str4;

  str3.sptr = &str1;
  str4.sptr = &str2;
}
I assume that's kind of what you're talking about anyway...

As for size during dynamic allocation. You tell malloc() how much memory to allocate so you should already know the size. You don't want to use sizeof() on a pointer to allocated memory because you'll only get the size of the pointer (e.g. 'char *ptr = malloc(10); printf("%d\n", sizeof(ptr));' will show 4, not 10 like you might expect). But if you have something like 'char buf[10]; printf("%d\n", sizeof(buf));' then it will show 10.
 
Old 05-14-2004, 11:32 PM   #3
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Original Poster
Rep: Reputation: 51
right, umm, i don't know the full size of the line, but it cant be bigger then a unsigned int , so i guess ill just tell it to do that .. but also the size of the line will change when its laoded..

hmm, does anyone think it will be better to let getline() make its own buffer, then copy its contents into a dynamic buffer?

and thx for the help with the structures, i think it should work with arrays since thats what i think ill be using to keep track of the other structures as they should be changing a lot

thx again

Last edited by SciYro; 05-14-2004 at 11:53 PM.
 
Old 05-15-2004, 10:53 AM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
the normal way to use getline() is like

Code:
size_t bufsiz = 0;
ssize_t linelen;
char* line = NULL;
while ((linelen = getline(&line, &bufsiz, infile)) != -1) {
        printf("Got a line length %d.  Here is the line:\n%s", linelen, line);
}
free(line);
There's an example in the man page too.
 
Old 05-15-2004, 11:53 AM   #5
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Well, personally, I think you should brush up a little on your C.
 
Old 05-15-2004, 05:24 PM   #6
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Original Poster
Rep: Reputation: 51
yep, looks like copying seems to be the best way for what i need

thx all
 
  


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
Is pointer a data-type or what aditya Programming 69 09-08-2017 02:09 PM
Data type conversion in C zaichik Programming 6 09-10-2005 05:47 PM
strange data type in c... HELP alaios Programming 9 09-03-2005 11:40 PM
MySQL data type question: timestamp versus integer to hold time vharishankar Programming 4 07-07-2005 09:01 PM
Data Type 9 bob Linux - Newbie 1 02-10-2001 08:10 AM

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

All times are GMT -5. The time now is 02:06 PM.

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