[SOLVED] C - How can you read a string into shmget and shmat?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!
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.
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.
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
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.