Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I have to add my thanks and appreciation for your wonderful site... People who frequent the site are the two best things - nice and smart..."

Geography

Where in the world do Tek-Tips members come from?

ANSI C structures, how to pass a value to

DougP (MIS)
11 Nov 10 8:59
here is my code if I hard code something it works fine. but that's not real world. I want to prompt for some values but I can't seem to get them to work. i.e. Enter your name and pass that to Asample.name

CODE

#include <stdafx.h>
#include <stdio.h>


int main( void )
{
    struct sample{
        char *name;
        char *addr;
        char *city;
        char *state;
        char *zip;
        char *phone;
    };
    struct sample Asample ;
    char thename;
    printf("Enter Name");
    scanf("%s",thename);
    //Asample.name = "Fred";  < this works

Asample.name= thename; //  this does not
    printf("%s\n",Asample.name);

   return 0; /* indicates successful termination */
} /* end main */

 

DougP
 

SDowd (Programmer)
11 Nov 10 11:04
char thename will only accept a single char, you would need to make it a char array or a char*.


http://www.cplusplus.com/reference/clibrary/cstdio/scanf/

 
DougP (MIS)
11 Nov 10 11:29
Ok so now I get these two errors
Warning    3    warning C4700: uninitialized local variable 'thename' used    21    

char *thename;

Warning    4    warning C4700: uninitialized local variable 'Asample' used    30    

CODE

// percentS.cpp : Defines the entry point for the console application.
//
#include <stdafx.h>
#include <stdio.h>

int main( void )
{
    int d;
    struct sample{
        char *name;
        char *addr;
        char *city;
        char *state;
        char *zip;
        char *phone;
    };
    struct sample Asample ;
    char *thename;       /* line 21 */
    printf("Enter Name");
    scanf("%s",thename);

Asample.name= thename;
    printf("%s\n",Asample.name);

    printf("%s\n",Asample.name);
    printf("%s\n",Asample.addr);
    printf("%s\n",Asample.city);  /* line 30 */
    printf("%s\n",Asample.state);
    printf("%s\n",Asample.zip);
    printf("%s\n",Asample.phone);

    printf("Enter a number");
    scanf("%d",&d);

   return 0; /* indicates successful termination */
} /* end main */

DougP
 

SDowd (Programmer)
11 Nov 10 11:37
same as in the other post:

CODE

char* thename = {0};

try defining the struct outside/above the definition of main
SDowd (Programmer)
11 Nov 10 11:42
also, you may want to create a constructor for the struct which defaults the members:

CODE

struct sample{
        char *name;
        char *addr;
        char *city;
        char *state;
        char *zip;
        char *phone;

        //constructor with initializer list(everything to 0)
        sample() :
        name(0),
        addr(0),
        city(0),
        state(0),
        zip(0),
        phone(0)
        {}
    };
DougP (MIS)
11 Nov 10 11:49
Program now crashes here is latest code

CODE

int main( void )
{
    int d;
    struct sample{
        char *name;
        char *addr;
        char *city;
        char *state;
        char *zip;
        char *phone;
    };
    struct sample Asample ={""} ;
    char *thename=NULL;
    printf("Enter Name");
    scanf("%s",thename);


Asample.name= thename;
    printf("%s\n",Asample.name);


    printf("Enter a number");
    scanf("%d",&d);

   return 0; /* indicates successful termination */
} /* end main */
(BTW I'm using Microsoft C++ now as my editor)
it compiles and runs I enter a name like Fred
then it crashes.
this is the code it brings up when it crashes, it stops on this line (the popup windows name is input.c)

CODE

#ifndef _UNICODE
                                    *(char *)pointer = (char)ch;  /* stops on this line */
                                    pointer = (char *)pointer + 1;

here is the description at the top of input.c
---------
*input.c - C formatted input, used by scanf, etc.
*
*       Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*       defines _input() to do formatted input; called from scanf(),
*       etc. functions.  This module defines _cscanf() instead when
*       CPRFLAG is defined.  The file cscanf.c defines that symbol
*       and then includes this file in order to implement _cscanf().
*
*Note:
*       this file is included in safecrt.lib build directly, plese refer
*       to safecrt_[w]input_s.c
-------------
 

DougP
 

SDowd (Programmer)
11 Nov 10 11:55
after these lines:

CODE

printf("Enter Name");
scanf("%s",thename);

add

CODE

printf("thename = %s\n", thename);

see if you can see what the actual value is prior to doing anything with the structure.
DougP (MIS)
11 Nov 10 11:56
it is crashing on this line
Asample.name= thename;

so we're back to square one!!!!

how to pass a string that is not hard coded to something???

DougP
 

SDowd (Programmer)
11 Nov 10 12:12
print out "thename" as soon as you do your scanf.  move the struct above the definition of "int main."  Include the default constructor with the initializer list.

in main:

CODE

sample Asample; //instance of defaulted struct

...
printf("Enter Name");
scanf("%s",thename);

//verify "thename" contains what you expect
printf("thename = %s\n", thename);

Then do the set to the struct:

CODE

Asample.name = thename;


 
DougP (MIS)
11 Nov 10 12:46
Thanks.


DougP
 
SDowd (Programmer)
11 Nov 10 14:21
Did any of that help fix it?
DougP (MIS)
11 Nov 10 14:29
nope  

DougP
 

SDowd (Programmer)
11 Nov 10 15:42
i changed "thename" to a char array and it worked for me:

CODE

// Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>


//struct definition
struct sample
{
    char *name;
    char *addr;
    char *city;
    char *state;
    char *zip;
    char *phone;

    //constructor with initializer list(everything to 0)
    sample() :
        name(0),
        addr(0),
        city(0),
        state(0),
        zip(0),
        phone(0)
        {
            //empty ctor body

        }
};



int _tmain(int argc, _TCHAR* argv[])
{
    //struct object
    sample Asample;

    char thename[100] = {0};
    printf("Enter Name\n");
    scanf("\t%s", thename, 4);


    printf("thename = %s\n", thename);

    Asample.name = thename; //  this does not
    printf("%s\n", Asample.name);


    return 0;
}

with output of:

CODE

Enter name
bob
the name = bob
bob
SDowd (Programmer)
11 Nov 10 16:25
As a note, the extra param of "4" in the call to scanf is not necessary.
cpjust (Programmer)
12 Nov 10 15:40

Quote (SDowd):

char* thename = {0};

NOTE:  This does NOT allocate any memory for the pointer.  It just sets the pointer to NULL.

Either use an array:

CODE

char thename[80];
or allocate some memory for the pointer:

CODE

char* thename = malloc( 80 * sizeof(char) );
SDowd (Programmer)
12 Nov 10 17:33
it is an array
cpjust (Programmer)
13 Nov 10 22:41
What's an array?

char* is a pointer.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close