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-08-2010, 11:18 PM   #1
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Rep: Reputation: 15
C - How can you read a string into shmget and shmat?


I want to read a string into shared memory using shmget and shmat. I'm using something like:
Code:
char *bookmark_strings, *s;

Book_Key = ftok(SHM_LOC,SHM_KEY);
shared_bookmarks = shmget(Book_Key, MAX_BOOKMARKS * sizeof(bookmarks), (0644 | IPC_CREAT));

bookmark_strings = shmat(shared_bookmarks, NULL, 0);
s = bookmark_strings;

*s = "hello";
I know that since it's a string I should be using something other then just char, but with something different shmat fails to be assigned. Can anyone help? Thanks!
 
Old 03-08-2010, 11:23 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
It's simple, but you'd really be doing yourself a favor by taking a look at "Beej's Guide to IPC":

http://beej.us/guide/bgipc/
 
Old 03-09-2010, 08:51 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
You also might want to brush up on the use of pointers in C. In particular, the statement
Code:
*s = "hello";
probably isn't doing what you want it to do.
I imagine you intended to make a copy of the literal string "hello" in the shared memory, but that would require something more like
Code:
 strcpy( s, "hello");
Your code writes the address of the literal string "hello" into shared memory, and I'm guessing, probably evokes one or more compiler errors or warnings.
--- rod.
 
1 members found this post helpful.
Old 03-09-2010, 05:16 PM   #4
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Original Poster
Rep: Reputation: 15
I'm having trouble getting the shared memory to act in the form of an array of strings (an array of char arrays).
Code:
char *bookmark_strings[10], *s[10], input_s[100];

Book_Key = ftok(SHM_LOC,SHM_KEY);
shared_bookmarks = shmget(Book_Key, MAX_BOOKMARKS * sizeof(bookmarks), (0644 | IPC_CREAT));

bookmark_strings = shmat(shared_bookmarks, (void *)0, 0);
s = bookmark_strings;
When I try to do something like this I get
Code:
error: incompatible types in assignment
Any suggestions? Thanks!
 
Old 03-09-2010, 05:52 PM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by golmschenk View Post
I'm having trouble getting the shared memory to act in the form of an array of strings (an array of char arrays).
Code:
char *bookmark_strings[10], *s[10], input_s[100];

Book_Key = ftok(SHM_LOC,SHM_KEY);
shared_bookmarks = shmget(Book_Key, MAX_BOOKMARKS * sizeof(bookmarks), (0644 | IPC_CREAT));

bookmark_strings = shmat(shared_bookmarks, (void *)0, 0);
s = bookmark_strings;
When I try to do something like this I get
Code:
error: incompatible types in assignment
Any suggestions? Thanks!
Code:
void *shmat(int shmid, void *shmaddr, int shmflg);
shmat returns a pointer and not an array of pointers.
Try the following:
Code:
bookmark_strings[0] = shmat(shared_bookmarks, (void *)0, 0);
This will not raise an error during compilation. Of course, this will only assign just one value to the first array element. You might want to rewrite your code to use a loop.
 
Old 03-09-2010, 06:09 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Okay, where to start? At the beginning, I suppose.
Code:
char *bookmark_strings[10]
Ten pointers to characters. Uninitialized.
Code:
bookmark_strings = shmat(shared_bookmarks, (void *)0, 0);
A pointer to void returned by a function. The pointer to void is assigned to ... the address of the zeroeth element of an array (of pointers to char), which is not an lvalue. Something wrong with that. Maybe you actually meant
Code:
bookmark_strings[0] = (char *)shmat(shared_bookmarks, (void *)0, 0);
Code:
s = bookmark_strings;
Once again, assignment to non-lvalue. Could you possibly have meant
Code:
s[0] = bookmark_strings[0]
? It might make sense if the previous line was per my suggestion.

I think very little of your problem has to do with shared memory, per se, just some missing understanding of the use of arrays and pointers in the C language. If I am wrong about that, then paulsm4's suggestion of reading Beej's Guide was already an outstanding pointer (pun intended).

--- rod.
 
Old 03-09-2010, 06:50 PM   #7
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Original Poster
Rep: Reputation: 15
Haha, great. Thanks everybody. I got everything working. Thanks!
 
  


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
read string after specific string from a text file using C++ programing language badwl24 Programming 5 10-08-2009 05:41 AM
read files from a folder and grep a string bhagirathi Programming 6 07-06-2009 06:27 AM
read string in c language dedexes Programming 5 03-05-2008 01:17 AM
To read a string into an array som_kurian Programming 7 12-06-2007 04:39 PM
Read a word in a string with bash orgazmo Programming 6 06-07-2005 10:19 AM

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

All times are GMT -5. The time now is 02:18 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