0

I have one of the those "assignment makes integer from pointer without a cast" question. I thought I casted the malloc correctly but it still seems to give me a warning.

I recently started C and starting to go deeper into pointers and mallocs.

the program is suppose to prompt for an integer (for how long the string is) then ask again for how many spaces before the prompted text) prompts for text)

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

#define         MAX_LINE        256

int     enterIntegerInRange     (int min, int max)
{
    char line[MAX_LINE];
    do{
       printf("Please enter an integer between 0-255: ");
       fgets(line,MAX_LINE,stdin);
      }
    while ((0 < min) || (255>max));
    }


void    enterText       (char*  text, int    length)
{
    printf("Please Enter some text: ");
    fgets(text,length,stdin);
}


char*   createField     (int fieldWidth, int numTabs,const char* text)
{
    int lengthText = strlen(text);
    char* toP = (char*)malloc(fieldWidth + 1);
    int i;
    for (i = 0; i < numTabs; i++){
        toP[i] = " ";}
    int j;
    for (j = 0; j < lengthText; j++){
        toP[numTabs + j] = text[j];}
    toP[i]='\0';
    return(toP);
}

int     main            ()
{
    char  text[MAX_LINE];
    int   fieldWidth;
    int   numTabs;
    char* fieldPtr;

   while  (1)
   {
      fieldWidth  = enterIntegerInRange(0,MAX_LINE-1);

      if  (fieldWidth == 0)
        break;

      numTabs     = enterIntegerInRange(1,fieldWidth-1);
      enterText(text,MAX_LINE);

      fieldPtr    = createField(fieldWidth,numTabs,text);

      printf("\"%s\"\n",fieldPtr);
      free(fieldPtr);
    }

    return(EXIT_SUCCESS);
  }

The problem is specifically is in the createField function:

toP[i] = " ";

I thought the taking in consideration for null would work.

Could you tell me what assignment makes an integer and how to go about fixing this?

YummyTree
  • 59
  • 8
  • 1
    You simply need to use `' '` rather than `" "`. And your problem is unrelated to `malloc` (but you shouldn't cast the result of `malloc` anyway). – Keith Thompson Oct 16 '14 at 20:35
  • possible duplicate of [Single quotes vs. double quotes in C](http://stackoverflow.com/questions/3683602/single-quotes-vs-double-quotes-in-c) – n. m. could be an AI Oct 16 '14 at 20:39

1 Answers1

1

You should assign a char, not a string:

toP[i] = ' ';

as your " " in the right hand side of the assignment is an array of two chars (' ' and '\0'), i.e. you are actually trying to assign a char * pointer to an element of toP array which is char.

afenster
  • 3,468
  • 19
  • 26
  • Ah thank you so much, Besides the fact I'm seg faulting and having other issues, the current one is solved! (Can compile fine) Thank you again! – YummyTree Oct 16 '14 at 20:47
  • I see a lot of places where it can/will segfault and at least one place where it will probably hang in a loop forever but please go ahead and fix all that staff by yourself. `printf` and your debugger are your friends here. – afenster Oct 16 '14 at 20:56