44 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 ...
user avatar
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 ...
user avatar
  • 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 ...
user avatar
  • 7,326
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 ...
user avatar
  • 7,326
4 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 ...
user avatar
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 ...
user avatar
  • 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 ...
user avatar
  • 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) ...
user avatar
  • 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 ...
user avatar
  • 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 ...
user avatar
  • 17.4k
2 votes
Accepted

Why Am I Having a Memory Leak?

This question was also posed on Facebook by you here: https://www.facebook.com/groups/cs50/permalink/343562319124153/ In a nutshell, GetString() mallocs space to hold the character array that you ...
user avatar
  • 18.3k
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 ...
user avatar
  • 1,359
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 ...
user avatar
  • 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.
user avatar
  • 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 ...
user avatar
  • 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 ...
user avatar
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 ...
user avatar
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 ...
user avatar
  • 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 ...
user avatar
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 ...
user avatar
  • 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 ...
user avatar
  • 18.3k
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 ...
user avatar
  • 18.3k
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).
user avatar
  • 18.3k
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 ...
user avatar
  • 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.
user avatar
  • 18.3k
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, ...
user avatar
  • 63.1k
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 ...
user avatar
  • 63.1k
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 ...
user avatar
  • 18.3k
1 vote
Accepted

Trouble Interpreting Linked List Sample Code - PSET5

first, I wanna make it clear that there is a difference between the address of a pointer and the address stored in a pointer (aka the address that the pointer points to). pointers are special type ...
user avatar
  • 17.4k
1 vote
Accepted

pset6 parse: char* pointers are right in parse but return wrong value

Notice the difference between the declaration of abs_path/query in main char abs_path[LimitRequestLine + 1]; char query[LimitRequestLine + 1]; vs. the declaration in parse. bool parse(const char* ...
user avatar

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