45 votes
Accepted

Difference between char and char* in c

The difference between char* the pointer and char[] the array is how you interact with them after you create them. If you are just printing the two examples, it will perform exactly the same. They ...
9 votes
Accepted

Pointer Concepts

First, recall that pointers are variables which contain an address to some other variable. So if you have a variable int* n, this defines a variable n, which contains the address of some place in ...
  • 2,625
6 votes
Accepted

PSET2: CRACK vs POINTERS

Your problem is that letters[i] is a char, but the crypt() function expects a char * (string) as its first argument. So what you really want to do is pass a string with a single character, not a ...
  • 7,356
6 votes
Accepted

Pointers and arrays, don't understand the relationship

OK, this is going to be long answer, because what you've asked spans many subjects, so bare with me. Arrays vs Pointers First, let's learn the difference between arrays and pointers. When you ...
  • 7,356
5 votes

Difference between char and char* in c

C99 N1256 draft There are two different uses of character string literals: Initialize char[]: char c[] = "abc"; This is "more magic", and described at 6.7.8/14 "Initialization": An array ...
4 votes

How can char* contain a collection of characters instead of a memory address?

what's going on under the hood? when having char *s = "hello"; basically an array of chars is created for you and is initialized with the contents of the string literal (in this case, "hello"). The ...
  • 17.4k
4 votes

Pointer Concepts

What is the difference between char word[length] and char *word = malloc(sizeof(char) * LENGTH)?? A char * is a pointer to a char (or a sequence of chars -- aka a string) while a char[] is an array ...
  • 17.4k
3 votes
Accepted

Pset4 - Recover (usage of sprintf)

Before I answer this question and important clarification must be made about what it means for something to "work". There is a term that will come up a lot when talking about C (and other languages) ...
  • 239
2 votes
Accepted

How can I avoid segfaults when sorting my linked list?

Regardless of whether the code does what it should do or not, the problem is likely to be caused by the fact that you are trying to access cursor->next when cursor is actually a null-pointer. This ...
  • 17.4k
2 votes

Dynamic string length allocation in new_node->word; array type char [46] is not assignable

You are confusing things here. First, according to the manual page of realloc Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc(). That is ...
  • 17.4k
2 votes
Accepted

Whats the adress of this string?

You can either do: char* str = "world"; In this case, world automatically will be stored in some chunk of memory and the adress of the first byte of this chunk will be stored in str. Using this ...
  • 1,369
2 votes
Accepted

Want to make sure I understand struct, malloc, and pointers and how they relate to each other

I originally didn't alloc for the struct, just declared it. It worked, and from what I understand, it worked because it was created in the stack? Am I correct? the type BITMAPINFOHEADER is not a ...
  • 17.4k
2 votes

3 days failing to get bubblesort working .. devastated :(

why do you use int* array[n]; and not int array[n];? you aren't calling your sort() function.
  • 425
2 votes
Accepted

Why does "if (input[i] == " ")" throw an error of "incompatible integer to pointer conversion"?

It turns out that using double quotes vs single quotes makes a difference in C! Double quotes are used to wrap a STRING, while single quotes are used to wrap a CHARACTER. This is easy to remember if ...
  • 253
2 votes

Why does this snippet of code work?

From the inside out: (arr + i) is a pointer to the i'th element of arr *(arr + i) is the contents of the pointer to i'th element of arr (dereferenced (arr + i)) &(*(arr + 1) is the address of ...
2 votes
Accepted

I have written this swap code but it is not compiling, Where's the bug

You don't need to specify the return type of your function when you call it, only when you declare/define it. So you've declared the function right, like this: void swap (int* a,int* b) But when you ...
2 votes
Accepted

pset4 resize file header question

*= and += (same for many other operators) are combined assignment operators. a += b is like a = a + b, a *= b is like a = a * b, and so on. In the line newbi.biWidth = bi.biWidth *= n; you also use ...
  • 20.7k
2 votes
Accepted

C: Vigenere - incompatible pointer types passing 'char **' to parameter of type 'const char *

The error comes from here: for(int l=0; l<strlen(argv); l++) Think about it. strlen() wants a const char* or char*. You gave it argv, which is an array of the command line arguments. Because argv ...
2 votes

Error with comparison between pointer and integer

What is ! = 2? Does not look like a valid expression to me. Use if (argc != 2). Instead of the other ! = 0, something like if (strlen(plaintext) != 0) would be correct, this condition would always ...
  • 20.7k
2 votes
Accepted

Difference between s and *s in pointer

printf("%c\n", *s); is the same as printf("%c\n", s[0]); By definition of printf, the %c format specifier in printf expects a character. So, *s says go to the address stored in s, and give me the ...
  • 18.4k
2 votes

pointers and scanf : when do I use &x, *x or just x?

I think you got it. scanf needs a memory address so it knows where to write to. Whenever you add & you get the memory address of what follows. But in case of strings, what we pass as a string ...
  • 20.7k
2 votes
Accepted

recover pset4 file handling

1 It's not a single char. You've declared an array of 8 chars, which is what you need to hold "000.jpg" {'0', '0', '0', '.', 'j', 'p', 'g', '\0'} 2 bookmark is an array. An array will "decay" to a ...
  • 18.4k
2 votes
Accepted

Using * and & both works

buffer is an array. Array variables "decay" into pointers when needed, so fwrite will understand buffer to mean a pointer to the first element (ie, the same as &buffer).
  • 18.4k
2 votes
Accepted

Printf doesn't print whole string "11111"

*a is same as a[0], pointing to the first element of a. Generally, *(a + i) is same as a[i]. I don't think you want an array of strings (string is a typedef of char*) all pointing to the same ...
  • 20.7k
2 votes
Accepted

vigenere output "incompatible pointer types"

string out[n]; You've declared an array of strings with this declaration. What you probably want is one string (ie, an array of chars). Change that to char out[n]; to get one string.
  • 18.4k
2 votes
Accepted

Pointer passed as argument, then suddenly goes null

Look at exactly what this code does: void setCharacter(char* pCharacter) { pCharacter = malloc(sizeof(char)); *pCharacter = 'b'; } Remember that things get passed by copy. First, ...
  • 64.6k
2 votes
Accepted

I know that my recover is wrong, but I'm not sure what steps to take-

There are several issues and areas for improvement here. First, there's this: FILE *f = fopen("argv[1]", "r"); By putting argv[1] in quotes, you're saying to treat whatever is in ...
  • 64.6k
2 votes
Accepted

incompatible pointer types error

create_family is the name of your function. In order to call it, be sure you don't forget the ( ) with the argument. If I wanted to create a family with generations number of generations, then I'd ...
  • 18.4k
2 votes
Accepted

PSET4 Blur not passing Check50

Without doing a deep dive, I do see a major problem in the logic. The code copies all the pixels from image to temp array. But then, it does all the calculations using the temp array as the source ...
  • 64.6k
1 vote
Accepted

pset5 Can't seem to get pointers right for `unload()`

It would have helped if you had told us exactly what the compile errors were. However, with a cursory look at the code, I see this: free *p; Your usage of free() is incorrect. For starters, it ...
  • 64.6k

Only top scored, non community-wiki answers of a minimum length are eligible