Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9202767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:19:43+00:00 2026-06-17T23:19:43+00:00

I am trying to attach an array of strings to the shared memory in

  • 0

I am trying to attach an array of strings to the shared memory in C. I have tried my best to attach the array of strings ( array1 and array 2 to the shared memory).

Here, array1 and array2 are arrays of strings of width 20 characters and size 5 ( How do I specify that in the attachment is also not very clear to me).
Also, a and b are 1-D integer and float arrays respectively, of size 5.

I want to change the state of the array of strings by updating their value at runtime, as I am doing.

#include <stdio.h>
#include <stdlib.h>
#include<sys/shm.h>
#define NUMBER_OF_DATA  5
int main()
{
   int size=(NUMBER_OF_DATA*(sizeof(int)+sizeof(float))) + (2*(20*NUMBER_OF_DATA));
   key_t key;
   key=ftok("/home/android/Desktop/newww.c",4);
   int shmid=shmget(key,size,0777|IPC_CREAT);

   int *a=(int *)shmat(shmid,0,0);
   float *b=(float *)(a+NUMBER_OF_DATA);
   char **array1=(char **)(b+NUMBER_OF_DATA);
   char **array2=(char **)(array1+(20*NUMBER_OF_DATA));
   int i;
   for(i=0;i<5;i++)
   {
       printf("enter value\n");
       scanf("%s",array1[i]);
   }
   shmdt(&shmid);
   shmctl(shmid,IPC_RMID,0);
   return 0;
}

My other process does the following

int shmid=shmget(key,size,0777|IPC_CREAT);


 int *a1=(int *)shmat(shmid,0,0);
  float *b1=(float *)(a1+NUMBER_OF_DATA);
  char **array11=(char **)(b1+NUMBER_OF_DATA);
  char **array22=(char **)((char *)array11+(20*NUMBER_OF_DATA));
  for(i=0;i<NUMBER_OF_DATA;i++)
    {
      a1[i]=aaa[i];
      b1[i]=bbb[i];
      array11[i]=array111[i];
      array22[i]=array2222[i];
    }

where aaa,bbb,array111 and array222 are other arrays from which the values are loaded into the shared memory by this process.
These 2 processes are together not helping me achieve what i wanted.

It would be great if someone could point out the reason and tell me the correct way to attach the array of strings to memory. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:19:45+00:00Added an answer on June 17, 2026 at 11:19 pm

    Let’s use a debugger to find where the error is happening. First compile with debugging turned on, then run it:

    $ gcc -g foo.c -o foo
    $ gdb foo
    
    GNU gdb 6.3.50-20050815 (Apple version gdb-1822) (Sun Aug  5 03:00:42 UTC 2012)
    Copyright 2004 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "x86_64-apple-darwin"...Reading symbols for shared libraries .. done
    
    (gdb) run
    Starting program: foo 
    Reading symbols for shared libraries +............................. done
    enter value
    12
    
    Program received signal EXC_BAD_ACCESS, Could not access memory.
    Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000
    0x00007fff928d3143 in __svfscanf_l ()
    

    The bt command, short for backtrace, will show where the error occurred:

    (gdb) bt
    #0  0x00007fff928d3143 in __svfscanf_l ()
    #1  0x00007fff928d0f6f in scanf ()
    #2  0x0000000100000e6b in main () at foo.c:20
    

    Here it’s line 20, calling scanf(). Let’s move up the stack to get into the right frame:

    (gdb) up
    #1  0x00007fff928d0f6f in scanf ()
    (gdb) up
    #2  0x0000000100000e6b in main () at foo.c:20
    20         scanf("%s",array1[i]);
    

    And now the p command, short for print, to examine values.

    (gdb) p array1
    $1 = (char **) 0x100034028
    (gdb) p i
    $2 = 0
    (gdb) p array1[i]
    $3 = 0x0
    

    Aha! The line scanf("%s", array1[i]) is trying to store a string to the value of array1[i]—0—rather than to its address.

    Let’s fix that by changing the line to:

       scanf("%s", &array1[i]);
    

    Now, recompile, and it works:

    $ ./foo
    enter value
    12
    enter value
    14
    enter value
    15
    enter value
    17
    enter value
    19
    

    However, there’s now a compiler warning on my machine:

    foo.c: In function ‘main’:
    foo.c:20: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
    foo.c:20: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
    

    But that’s another question for you to figure out 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question related to this one : I'm trying to attach an
I am trying to create an array with different hrefs to then attach to
I have a fairly tricky situation that I'm trying to determine the best design
I am trying to attach the validation on a button click. I have: <script>
I am working with shared memory and hence I need to attach some portion
I am having this array. Last few hours I have been trying to loop
I am trying to attach the Netbeans profiler to my Java project but it
I am trying to attach onBlur and onFocus handler to a SSN input field.
I'm trying to attach my database on a server that is running SQL2005 and
I'm trying to attach < Previous and Next > links to a jQueryUI datepicker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.