Malloc and string pointer problem

assignment makes integer from pointer without a cast

Page 1 of 1

2 Replies - 1877 Views - Last Post: 29 October 2010 - 08:35 PM Rate Topic: -----

#1 FringeOfChaos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-July 10

Malloc and string pointer problem

Posted 29 October 2010 - 06:38 PM

I ran a search through the forums, but the only matches didn't quite answer my question, so I'll post a new one.

When I try to compile the following code (GREATLY trimmed down to the necessities) with GCC, I get the warnings below:

typedef struct {
    int day, month, year;
} DATE;

typedef struct {
    int code;
    DATE birth, performance;
    float ebird, reg;
    double time;
    char series, expertise;
    char state[2];
    char* first, last, title;
} SPKR;

...
...

void getFirstName( SPKR spkr )
{
    int max = 512;
    int read;
    char *temp;
    
    temp = (char*) malloc(max * sizeof(char));
    printf("Please enter the first name of the speaker.\n");
    printf("First name: ");
    read = getline(&temp, &max, stdin);
    spkr.first = (char*) malloc((read + 1) * sizeof(char));
    strcpy(spkr.first, temp);
}

void getLastName( SPKR spkr )
{
    int max = 512;
    int read;
    char *temp;
    
    temp = (char*) malloc(max * sizeof(char));
    printf("Please enter the last name of the speaker.\n");
    printf("Last name: ");
    read = getline(&temp, &max, stdin);
    spkr.last = (char*) malloc((read + 1) * sizeof(char));
    strcpy(spkr.last, temp);
}

void getTitle( SPKR spkr )
{
    int max = 512;
    int read;
    char *temp;
    
    temp = (char*) malloc(max * sizeof(char));
    printf("Please enter the title of the speech or event.\n");
    printf("Title: ");
    read = getline(&temp, &max, stdin);
    spkr.title = (char*) malloc((read + 1) * sizeof(char));
    strcpy(spkr.title, temp);
}
...
...




The warnings I get from GCC are the following:

warning: assignment makes integer from pointer without a cast
warning: passing argument 1 of strcpy makes pointer from integer without a case

This is also the case for a function that fills in the last name, and for the title, but NOT the first name. The odd thing is, when I put my struct like this...

typedef struct {
    int code;
    DATE birth, performance;
    float ebird, reg;
    double time;
    char series, expertise;
    char state[2];
    char* first;
    char* last;
    char* title;
} SPKR;



It works fine and without complaint. Is it a quirk of pointers that makes me unable to list them out like I did at first? Listing them out each on their own line is the only solution I've been able to find (and that through experimentation).

I've technically solved the problem, but the why is going to bother me until I can fix it: I don't want to make a similar mistake in the future.

Is This A Good Question/Topic? 0
  • +

Replies To: Malloc and string pointer problem

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: Malloc and string pointer problem

Posted 29 October 2010 - 06:50 PM

This line:
char* first, last, title;


Only first is a pointer last and title are type char (not char pointer).

This would be correct:

char* first, *last, *title;


Also in future please post the entire error messages. The message will contain the line numbers of the problem.

Jim

This post has been edited by jimblumberg: 29 October 2010 - 06:52 PM

Was This Post Helpful? 1
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Malloc and string pointer problem

Posted 29 October 2010 - 08:35 PM

// you pass a value.
// this is a copy of the struct you pass
void getFirstName( SPKR spkr ) {
	//...
	spkr.first = (char*) malloc((read + 1) * sizeof(char));
	strcpy(spkr.first, temp);
	// and now drop spkr
	// congrats on your memory leak
}



Perhaps you want something like:
#define MAX_BUFF 512
char *getStringFromUser(const char *prompt) {
	char temp[MAX_BUFF];
	int size = MAX_BUFF;
	char *s = NULL;
	
	printf(prompt);
	if (getline(&temp, &size, stdin)!=-1) { 
		s = malloc(size+1);
		strcpy(s, temp);
	}
	return s;
}

void getFirstName( SPKR *spkr ) {
	spkr->first = getStringFromUser("Please enter the first name of the speaker.\nFirst name: ");
}

void getLastName(SPKR *spkr) {
	spkr->last = getStringFromUser("Please enter the last name of the speaker.\nLast name: ");
}


Was This Post Helpful? 2
  • +
  • -

Page 1 of 1