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

  • Home
  • SEARCH
  • 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 163241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:31:58+00:00 2026-05-11T11:31:58+00:00

As was suggested in an answer to my last question ( How do I

  • 0

As was suggested in an answer to my last question (How do I send an array of integers over TCP in C?), I tried to send an array of long int, however I may be doing something to break the solution…

#define ARRAY_LEN 4  /* I'm using long because the numbers are very large,  * but in this example they're small to save space. */ long originalArray[ARRAY_LEN] = { 1, 2, 3, 4 };  myObject.SetMyArray(originalArray);  // NOTE: The following is in a different function.  long *myArrayFromFunction = myObject.GetMyArray();  write(clientSocketFD, myArrayFromFunction, sizeof(myArrayFromFunction) * ARRAY_LEN); 

Is converting to a pointer then passing incorrect?

When reading on the client side, instead of getting the numbers I sent (1, 2, 3, 4), I get long numbers such as 140088443806649…

#define ARRAY_LEN 4  long targetArray[ARRAY_LEN]; read(socketFD, targetArray, sizeof(targetArray) * ARRAY_LEN); 

So, assuming that I need to read into a pointer, I tried this…

#define ARRAY_LEN 4  long *targetArray; read(socketFD, targetArray, sizeof(targetArray) * ARRAY_LEN); 

But this didn’t work either (the read function returned -1).

  • 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. 2026-05-11T11:31:59+00:00Added an answer on May 11, 2026 at 11:31 am

    The extra pointer is unnecessary. The array (which basically is a pointer) can be passed directly. You are incorrectly using the size of the pointer instead of the size of the data type (long); use sizeof (long) * ARRAY_LEN or just sizeof (originalArray) instead.

    Write:

    #define ARRAY_LEN 4 long originalArray[ARRAY_LEN] = { 1, 2, 3, 4 }; write(clientSocketFD, originalArray, sizeof (originalArray)); 

    Read:

    #define ARRAY_LEN 4 long targetArray[ARRAY_LEN]; read(socketFD, targetArray, sizeof (targetArray)); 

    If you are passing the array as a pointer, then sizeof cannot be used to get the total size of the array. In that case, the size must be passed along with the pointer and the size constructed with sizeof (long) * ARRAY_LEN.

    Be careful using a type such as long since it is not always the same size on different platforms (i.e. 32-bit versus 64-bit); favor sized types like int32_t instead. Also, you can run into issues with endianess as well. Consider byte-swapping the values with htonl before writing.

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

Sidebar

Related Questions

In a previous question I asked, the suggested answer was for me to partition
In another question , the accepted answer suggested replacing a (very cheap) if statement
In his answer to this question , BlackBear suggested replacing string y = Session[key]
A recent answer to a question on converting Java to C# suggested I should
The only similar question to this I found was this and the answer suggested
UPDATE!!! Suggested answer is NOT correct, my mistake. The #container DIV should've had float:left;.
This answer suggested i should put my data in JS instead of a textarea.
In a recent question on stubbing, many answers suggested C# interfaces or delegates for
This answer to a question about C++ unit test frameworks suggests a possibility that
Take the following demo code (from the GIO answer to this question), which uses

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.