Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ANSI C structures, how to pass a value to

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
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
 
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
 
same as in the other post:

Code:
char* thename = {0};

try defining the struct outside/above the definition of main
 
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)
        {}
    };
 
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
 
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.
 
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
 
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
[COLOR=red]printf("thename = %s\n", thename);[/color]

Then do the set to the struct:

Code:
Asample.name = thename;


 
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
 
As a note, the extra param of "4" in the call to scanf is not necessary.
 
SDowd said:
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) );
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top