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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:16:15+00:00 2026-06-16T01:16:15+00:00

I have a string which I need to pass to a function like below.

  • 0

I have a string which I need to pass to a function like below.

scanf("%s%d",&name,&telno);
addatend(telno,name);

where

char name[MAX];

I am getting the following warning :

warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[50]’ [-Wformat]

I did try to do changes like having one more char pointer and point to name. But the warning stays.

In the function I have strcpy

strcpy(addnode->name,name1);

This is coping the name from main to the name in the structure addnode. But there is a segmentation fault at this line.

I tried to check in splint (did not have any issues at this line) and valgrind but could not identify my mistake. Hence came here for help. Thanks in advance..

The code:

typedef struct Mystruct{
char *name;
int telno;
struct Mystruct *nextp;
}data;

void addatend(int telno1, char* name1)
{
    data *addnode, *previousnode;
    addnode = malloc (sizeof(data));

    if(addnode == NULL)
    {
        printf("Memory allocation failed...Exiting");
        exit(0);
    }

    strcpy(addnode->name,name1);        //error Part of the code.
  • 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-16T01:16:17+00:00Added an answer on June 16, 2026 at 1:16 am

    When reading a string with scanf, the array is passed to the function as a pointer, so you don’t need the address-of operator &.


    Regarding your second problem… You need to allocate memory for the destination you copy to as well. Either use malloc again (using strlen(...) + 1, the +1 for the string terminator) before the strcpy, or use strdup which allocates and copies the string:

    addnode->name = strdup(name1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function like below void functionA(unordered_map<string, classA*>* arg1); I need to pass
So I have a flashobject which I need to pass a formatted DateTime string
I have a string which I need to verify if it's a Country code.
I have a string data which I need to parse into a dictionary object.
Hi have this URL string which I need to extract possibly using regex but
I have a string input from which I need to extract simple information, here
i have a string which has the following format (3,1,Mayer,Jack). I need all 4
I have a long string (multiple paragraphs) which I need to split into a
I have a starttime as textfield. I need to covert the String which I
i have a string and i need a utility method which looks for bad

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.