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 7913683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:54:04+00:00 2026-06-03T13:54:04+00:00

This is probably something simple, but I can’t figure out why I can’t get

  • 0

This is probably something simple, but I can’t figure out why I can’t get the value from this char*.

Here is my issue:

static char* DIR_ENTRY_PATH = NULL;

…

while (1) // infinite loop

    {

      // accept a client connection 

      clientFd = accept (serverFd, clientSockAddrPtr, &clientLen);

      if (fork() == 0) // Create child proc to get dir entry info 

        {

          //read dir entry info     
readInfo(clientFd);
printf("dpath: %s\n", DIR_ENTRY_PATH); //prints out the the correct value (DIR_ENTRY_PATH set in readInfo)
int test = 1;
      //get dir entry info and write back
      DIR *dir;
      struct dirent *entry; //pointer to dir entry
      struct stat stbuf; //contains file info

      if((dir = opendir(DIR_ENTRY_PATH)) == NULL){ //make sure entry is valid
        printf("error with dirent\n");
            exit(1);
      }
      else{
        printf("gathering directory entry info...\n");
        while((entry = readdir(dir)) != NULL){
            char *entryname = entry->d_name;
printf("path: %s\n", DIR_ENTRY_PATH); /*prints nothing out.. */ - PROBLEM IS HERE
printf("int: %d\n", test); //*prints 1 out.. */

…

readInfo():

//reads info from the client
void readInfo(int fdesc){
  int fd = fdesc;
  char str[200];

  readLine(fd, str); //read line in from socket
  DIR_ENTRY_PATH = str; 
  printf("received path: %s\n", DIR_ENTRY_PATH); //displays correct value
}
//reads a single line
int readLine(int fdesc, char *strng){
 int fd = fdesc;
 char *str = strng;
 int n;

 do{
   n = read(fd,str, 1); //read a single character
 }while(n > 0 && *str++ != 0);
 return (n>0); //return false if eoi
}//end readLine

Why am I able to get the value of the test int but not the dir_entry_path? Thanks for any help.

  • 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-03T13:54:06+00:00Added an answer on June 3, 2026 at 1:54 pm

    You are assigning a pointer to a local variable to your global variable, but once the function returns, the local variable is gone!

    void readInfo(int fdesc){
      int fd = fdesc;
      char str[200];             // Local variable
    
      readLine(fd, str);
      DIR_ENTRY_PATH = str;      // Pointer assigned to global
      printf("received path: %s\n", DIR_ENTRY_PATH); //displays correct value
    }
    

    After the function returns, the local variable is undefined, and its storage can be reused by the next function, etc.

    How to fix the problem? There are many possible ways, but the simplest might be:

    static char DIR_ENTRY_PATH[1024];
    
    void readInfo(int fdesc){
      int fd = fdesc;
      char str[200];                  // Local variable
    
      readLine(fd, str);
      strcpy(DIR_ENTRY_PATH, str);    // Copy local string into global string
    
      printf("received path: %s\n", DIR_ENTRY_PATH); //displays correct value
    }
    

    As a stylistic point, ALL_CAPS normally denotes a macro, or an enum value (because enum is a bit like #define which uses ALL_CAPS).

    I hope you have proper bounds checking on readLine(). I got lazy with my modification and simply ensured the global was (five times) longer than the local variable. Tune to suit yourself. I’d also use an enum for the buffer size (and a lower-case name instead of DIR_ENTRY_PATH).

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

Sidebar

Related Questions

I know this is probably something simple, but I can't figure it out. When
This is probably something extremely simple but I can't get my head around it
this is probably something stupid, but i can't figure it out. I'm getting a
I've probably overlooked something simple, but I can't figure out how to convert a
This is probably something simple, but I can't seem to find the answer. I
Hi everyone this is probably something extremely simple that i'm overlooking but can someone
I'm probably missing something simple here, but I can't find the answer elsewhere. I
this is probably so simple but still i can't get it to work i'm
I cant figure out why this isn't working. It's probably something simple. The iframe
This is probably something simple, but I just can't see it. I've got my

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.