0

I'm working in a language called Sedona that can take native C methods. In order to integrate C in sedona, the variable declarations are a bit off.

  1. Sedona -> C
    • bool -> int32_t
    • bool[] -> uint8_t*
    • byte -> int32_t
    • byte[] -> uint8_t*
    • short -> int32_t
    • short[] -> uint16_t*
    • int -> int32_t
    • int[] -> int32_t*
    • long -> int64_t
    • long[] -> int64_t*
    • float -> float
    • float[] -> float*
    • double -> double
    • double[] -> double*
    • Obj -> void*
    • Obj[] -> void**
    • Str -> uint8_t*
    • Str[] -> uint8_t**

My method is trying to open a file, read its contents and return the contents of the file as a string for the other Sedona method to use. I know most of you probably don't know Sedona, but I am getting some errors that I don't understand. Here is my code:

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

Cell MyWeblet_MainWeblet_getFile(SedonaVM* vm, Cell* params){
    uint8_t* file_name = params[1].aval;
    FILE *fp;
    uint8_t* fileContents;
    struct stat st;
    stat(&file_name, &st);
    int32_t size = st.st_size;
    int32_t itter = 0;
    Cell result;

    fileContents = malloc(sizeof(char)*size);
    fp = fopen(file_name, "r"); //read mode

    if (fp==NULL){
        perror("Error while opening the file.\n");
        exit(EXIT_FAILURE);
    }

    unit8_t* ch;
    while ((ch = fgetc(fp))!=EOF){
        fileContents[itter] = ch;
        itter++;

    }
    result.aval = fileContents;
    fclose(fp);
    return result;
}

I'm getting more errors than this, but here is an example of what pops up:

- warning C4047:'function' : 'const char *' differs in levels of indirection from 'uint8_t **'
- warning C4024:'stat' : different types for formal and actual parameter 1
- error C2275: 'int32_t' : illegal use of this type as an expression

I really just want to understand what those errors mean, I don't need anyone to fix the code for me (although suggestions would be nice).

user3516302
  • 81
  • 1
  • 1
  • 9
  • A few extra tips: you should make sure to check the return value of `stat()`, since it can fail for many reasons. `sizeof(char)` is guaranteed to be 1 by the C standard, so there's no need to multiply by it. You can replace the entire `while` loop with a single call to [`fread(3)`](http://linux.die.net/man/3/fread) (plus error checking). You should also check to make sure that `malloc()` succeeds, and don't forget to `free()` it if another error occurs to avoid leaking (like if the file fails to open), although in this case you're exiting right away so that's not a big concern. – Adam Rosenfield Jun 27 '14 at 18:52

2 Answers2

1

In the line,

stat(&file_name, &st);

the argument &file_name is not appropriate. The type of the first argument to stat is char const*. The type of &file_name is uint8_t**.

If char and uint8_t are the same type in your platform, you can use:

stat(file_name, &st);
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

Difference in level of indirection occurs when you assign a pointer variable to a non-pointer type, or more generally speaking when the number of indirections (stars) in the pointer variables differ. For example, you get this error in the following (de)referencing cases.

int *x, y;
x = y;  // correct use: x = &y;
y = x;  // correct use: y = *x;

In particular, the error you are getting shows a mismatch between the formal and actual parameters. This happens during a function call when the argument values that you pass on from a caller to a callee involves a difference in the indirection level. An example follows.

void swap(int* x, int *y) { ... }
int x = 2; y = 3;
swap(x, y);  // correct use: swap(&x, &y);

Unfortunately, C is not a strongly typed language and hence simply throws off warnings for these types of errors. C++ however, reports these as errors.

Debasis
  • 3,680
  • 1
  • 20
  • 23