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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:41:49+00:00 2026-06-01T04:41:49+00:00

In the code below i was expecting another output! : #include <stdio.h> #include <stdlib.h>

  • 0

In the code below i was expecting another output! :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct _cust {
        char    customerId[10];
        char    customerPhone[10];
        char    customerDep[4];
} cust;

int main(int argc, char **argv) {
        cust    *newCust;
        const char testChar[] = "11W35A5CT-012345678-CORP";

        newCust = (cust *)malloc(sizeof(struct _cust));
        newCust = (cust *)testChar;

        printf("CustomerId = %s\n", newCust->customerId);
        printf("CustomerPhone = %s\n", newCust->customerPhone);
        printf("CustomerDep = %s\n", newCust->customerDep);

        return 0;
}

And the output is :

CustomerId = 11W35A5CT-012345678-CORP
CustomerPhone = 012345678-CORP
CustomerDep = CORP

I was expecting this output :

CustomerId = 11W35A5CT-
CustomerPhone = 012345678-
CustomerDep = CORP

Can someone explain to me why this? Thanks.

EDIT :

To avoid confusing on my post i’m adding here the gdb trace when debugging this program:

(gdb) b main
Breakpoint 1 at 0x8048474: file name.c, line 11.
(gdb) run
Starting program: /home/evariste/src/customer_files/a.out 

Breakpoint 1, main (argc=1, argv=0xbffff2c4) at name.c:11
11  int main(int argc, char **argv) {
(gdb) n
13      const char testChar[] = "11W35A5CT-012345678-CORP";
(gdb) n
15      newCust = (cust *)malloc(sizeof(struct _cust));
(gdb) n
16      newCust = (cust *)testChar;
(gdb) n
21      printf("CustomerId = %s\n", newCust->customerId);
(gdb) print *newCust
$1 = {customerId = "11W35A5CT-", customerPhone = "012345678-", 
  customerDep = "CORP"}

So whey here i see that customerId = “11W35A5CT-” and when i try printf i got the whole string?

  • 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-01T04:41:51+00:00Added an answer on June 1, 2026 at 4:41 am

    printf() will output until it hits a \0 which signals the end of the string. There’s no \0 after any of the hyphens and so printf() will print from the start position you gave it to the end of what’s in testChar.

    Also, you leaked away the memory the call to malloc allocated for you. Perhaps you want to copy the string into the struct?

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

Sidebar

Related Questions

Using the code below I'm expecting a green rectangle and a red one of
I wrote the code below and I was expecting that, when the first loop
The code below returns an address when executed in Windows, though I was expecting
I'm expecting the code below to print 1 but its printing a random large
I am running the simple code below and expecting the depth of o to
This code below is for executing ls -l | wc -l. In the code,
I am executing below code successfully without any errors but when I am executing
Executing the below code gives me the following exception on the last line: InvalidOperationException:
I am exporting an .xls file into datatable. Below are my code: private DataTable
I am creating the below table dynamically using jQuery... After executing my code I

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.