1

I'm working on a personal project to learn C. The first part of it is to parse a user-prompted line of integers separated by white spaces and read it into an array of integers. Here's what I have:

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

#define MAX_PAGES 100

int main() {

char *pageInputArray;
int *pages[MAX_PAGES];
char *start;
char *end;
int iterVar;


printf("%s\n", "Enter the page reference stream: ");
scanf("%s", pageInputArray);

start = pageInputArray[0];

iterVar = 0;
while ((start = strpbrk(start, "0123456789") != NULL) && (iterVar < MAX_PAGES))  {
    pages[iterVar] = strtol(start, &end, 0);
    printf("%d\n", pages[iterVar]);
    start = end;
    iterVar++;
}
}

And when I try to complile I get this warning:

warning:  assignment makes pointer from integer without a cast [enabled by default]

For the lines which have:

start = pageInputArray[0];

and

while ((start = strpbrk(start, "0123456789") != NULL) && (iterVar < MAX_PAGES))  {

and

pages[iterVar] = strtol(start, &end, 0);

It's driving me crazy because it seems that the machine thinks @start is an integer, but I'm clearly declaring it as a char* . Can someone tell me what's going on and how to fix it?

3 Answers 3

1

you need to allocate pageInputArray first before storing anything in it

1
  • I tried making that change and it didn't work. How do you mean?
    – newalchemy
    Commented Apr 11, 2014 at 19:20
0
#include <stdio.h>
#include <string.h>

#define MAX_PAGES 100

int main() {
    char pageInputArray[MAX_PAGES + MAX_PAGES];
    int pages[MAX_PAGES];
    char *start;
    char *end;
    int iterVar;

    printf("Enter the page reference stream: \n");
    fgets(pageInputArray, sizeof pageInputArray, stdin);

    start = &pageInputArray[0];
    iterVar = 0;
    while ((start = strpbrk(start, "0123456789")) != NULL && iterVar < MAX_PAGES)  {
        pages[iterVar] = strtol(start, &end, 0);
        printf("%d\n", pages[iterVar]);
        start = end;
        iterVar++;
    }
    return 0;
}
3
  • Wow, that works perfect! Thanks! Out of curiosity, why did you allocate pageInputArray to be the size of [MAX_PAGES + MAX_PAGES] ?
    – newalchemy
    Commented Apr 11, 2014 at 19:26
  • @newalchemy E.g 3 Element : 1 2 3. 3 Element + 2 space (separator) + null-terminator = 3+3.(Minimal memory)
    – BLUEPIXY
    Commented Apr 11, 2014 at 19:32
  • Makes a lot of sense. Thanks a lot!
    – newalchemy
    Commented Apr 11, 2014 at 20:27
0

start is a pointer to type char what you are assigning it is a value. I mean pageInputArray[0] has a value of type char.

Not the answer you're looking for? Browse other questions tagged or ask your own question.