I am trying to calculate the size of char *. I would would think one of these 2 methods would work but they are not.

char *vertices;
printf(strlen(vertices));
printf(sizeof(vertices));

Well vertices is just a pointer to a memory location and, as far as I know, doesn't have any value until assigned. Are you trying to get the length of the memory address, or how many bits a char is (8 bits)?

Sorry I thought it was a simple pointer issue so I didn't elaborate very much. I first used malloc to allocate some space. Then I use this if statement to fill vertices with some char values. After that I add a null character at the end so it is properly terminated like char arrays are supposed to be.

vertices = malloc(1000);
if(counter - i == 1 && flag == 0)
{
    vertices[addcounter] = (char)c;
    //printf("The vertice is %c :\n", vertices[addcounter]);
    addcounter++;
}
vertices[addcounter] = '\0';

I am trying to calculate the size of char *

sizeof() won't give you what you want, all sizeof gives you is sizeof(char*), not the number of bytes allocated to the pointer. All 32-bit compilers on MS-Windows and *nix machines sizeof(char*) is a constant value of 4, same as sizeof(long).

The only way sizeof will work for you is to do something like this:

char vertices[1000] = {0};
printf("sizeof(vertices) = %d\n", sizeof(vertices));

Edited 3 Years Ago by Ancient Dragon

I first used malloc to allocate some space. Then I use this if statement to fill vertices with some char values. After that I add a null character at the end so it is properly terminated like char arrays are supposed to be.

Then strlen should work. Could you post a small but complete example that exhibits the problem?

strlen() will only return the length of the string, not the number of bytes allocated to the pointer. So if malloc allocates 1000 bytes, but you only copy "Hello" into the memory, strlen() will only return the number of characters in "Hello", not 1000. There is no standard C function that will return the number of bytes allocated to the pointer which is why you need to save it yourself in another integer if you need it later for something.

Edited 3 Years Ago by Ancient Dragon

@deceptikon
I'm sorry I can't because it gives a segmentation fault. And it happens right where I try to get the strlen and sizeof. These are the warnings if they help.

gcc -Wall *.c
open_file_c1.c: In function ‘main’:
open_file_c1.c:151:5: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [enabled by default]
/usr/include/stdio.h:361:12: note: expected ‘const char * restrict’ but argument is of type ‘unsigned int’
open_file_c1.c:152:5: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [enabled by default]
/usr/include/stdio.h:361:12: note: expected ‘const char * restrict’ but argument is of type ‘int’

How do you quote people I don't see a way to do that.

@Ancient Dragon
I'm trying to get something similar to "hello" like you mention in your second post. How would I get the length of the string? I know I have a valid string because of this. I'm able to print through with a for loop and print it as a string.

for(i = 0; i < 100; i++)
{
    printf("The vertice is %c :\n", vertices[i]);
}

printf("The vertice is %s :\n", vertices);

There's something else wrong -- the warnings you posted don't correspond to the code you just posted. There is nothing wrong with those two printf() statements. What exactly are lines 151 in open_file.c? Better yet, just post the entire code in open_file.c.

This article has been dead for over six months. Start a new discussion instead.