error: expected identifier or '(' - c programming language - cs50

I've been wracking my brain trying to figure this out for hours. I'm getting the following error for the code below:
mario.c:5:1: error: expected identifier or '('
#include <cs50.h>
#include <stdio.h>
int main(void);
int blocks = 0;
do
{
int blocks = get_int();
printf("%d\n", blocks);
}
while (blocks > 23);
Anyone know what I'm doing wrong here?

Remove the ; after the int main(void) declaration
int main(void)
{
...
}

Related

Phone book management in C

Task: to implement a Phone book management in C
What I did not understand exactly: I have to implement qsort to sort the phone book (lexicographically by last name and first name) which didn´t work although I used the syntax exactly as it is displayed on the web page: https://www.tutorialspoint.com/c_standard_library/c_function_qsort.htm
The error message:
c:41:25: error: expected ')' before numeric constant
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char* vorname;
char* nachname;
char* telefonnummer;
} telefonbucheintrag;
void schreibe_eintrag(
telefonbucheintrag* eintrag_ptr,
char* vorname,
char* nachname,
char* telefonnummer)
{
eintrag_ptr->vorname = malloc(strlen(vorname)+1);
eintrag_ptr->nachname = malloc(strlen(nachname)+1);
eintrag_ptr->telefonnummer= malloc(strlen(telefonnummer)+1);
strcpy(eintrag_ptr->vorname, vorname);
strcpy(eintrag_ptr->nachname, nachname);
strcpy(eintrag_ptr->telefonnummer, telefonnummer);
}
int main()
{
telefonbucheintrag telefonbuch[100];
int32_t telefonbucheintraege = 0;
schreibe_eintrag(telefonbuch+0, "Ada", "Lovelace", "004917155669988");
schreibe_eintrag(telefonbuch+1, "Alan", "Turing", "004917155669922");
schreibe_eintrag(telefonbuch+2, "Ingo", "Mueller", "004917155669911");
schreibe_eintrag(telefonbuch+3, "Ilse", "Mueller", "004917155669933");
schreibe_eintrag(telefonbuch+4, "Stefan", "Sadat", "004917155669988");
telefonbucheintraege = 5;
qsort (telefonbuch, 5, sizeof(int), telefonbucheintraege );
}
replace sizeof(int) with sizeof(*telefonbuch) or what you have

ICU4C austrdup function

I'm trying to run the code demo for ICU4C bellow, and getting
warning: implicit declaration of function 'austrdup'
which subsequently generate an error. I understand that this is due to the missing imported library that contains 'austrdup' function, and have been looking at the source code to guess which one it is, but no luck. Does anyone have any idea which one should be imported?
#include <unicode/umsg.h>
#include <unicode/ustring.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[])
{
UChar* str;
UErrorCode status = U_ZERO_ERROR;
UChar *result = NULL;
UChar pattern[100];
int32_t resultlength, resultLengthOut, i;
double testArgs[] = { 100.0, 1.0, 0.0};
str=(UChar*)malloc(sizeof(UChar) * 10);
u_uastrcpy(str, "MyDisk");
u_uastrcpy(pattern, "The disk {1} contains {0,choice,0#no files|1#one file|1<{0,number,integer} files}");
for(i=0; i<3; i++){
resultlength=0;
resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, testArgs[i], str);
if(status==U_BUFFER_OVERFLOW_ERROR){ //check if output truncated
status=U_ZERO_ERROR;
resultlength=resultLengthOut+1;
result=(UChar*)malloc(sizeof(UChar) * resultlength);
u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, testArgs[i], str);
}
printf("%s\n", austrdup(result) ); //austrdup( a function used to convert UChar* to char*)
free(result);
}
return 0;
}
austrdup is not an official ICU method. It's only used by tests in ICU and defined in icu4c/source/test/cintltst/cintltst.h and implemented in icu4c/source/test/cintltst/cintltst.c. It is bascially just a wrapper around u_austrcpy.

why do i get "error C2678: binary '==' : no operator found which takes a left-hand operand of type"

I am using vs2012 and having the same problem. below is my sample source code.
#include "stdafx.h"
#include "iostream"
#include "list"
#include "algorithm"
#include "xutility"
using namespace std;
typedef struct Student{
string name;
int age;
int operator==(list<Student>::iterator itr)
{
return (!strcmp(itr->name.c_str(),this->name.c_str()));
}
}SStudent;
int _tmain(int argc, _TCHAR* argv[])
{
list<SStudent> myList;
SStudent temp1;
temp1.age = 10;
temp1.name = "aaaa";
myList.push_back(temp1);
list<SStudent>::iterator itr;
itr = std::find(myList.begin(), myList.end(), "aaaa");
return 0;
}
here is the error:
Error 1 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Student' (or there is no acceptable conversion) c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 3186

Problems when reading integers with scanf

I know that it might be a stupid question but can you tell me why the following patch of code fails? I see nothing wrong. I am trying to read integers using scanf. I have included the necessary library, but when I run the program it crashes after I read the first s. Thank you.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <vector>
using namespace std;
int main()
{
int n, x;
scanf("%d", &n); scanf("%d", &x);
vector< pair<int, int> > moments;
for(int i = 0; i < n; ++i)
{
int f, s;
scanf("%d", &f);
scanf("%d", &s );
moments[i].first = f;
moments[i].second = s;
}
return 0;
}
That is not the way to assign values to moments since moments[i] does not yet exist. Try:
pair<int, int> thing;
thing = make_pair(f,s);
moments.push_back(thing);
instead of your assignements to moments elements.

writing my first exploit in linux

How can I modify the source code in the func( ) so that the address to which the program returns after executing func () is changed in such a manner that the instruction printf("first print\n”) is skipped. Use the pointer *ret defined in func() to modify the return address appropriately in order to achieve this.
Here is the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void func(char *str)
{
char buffer[24];
int *ret;
strcpy(buffer,str);
}
int main(int argc, char **argv)
{
if (argc < 2)
{
printf("One argument needed.\n");
exit(0);
}
int x;
x = 0;
func(argv[1]);
x = 1;
printf("first print\n");printf("second print\n");
}
As sherrellbc noted, a program's exploits are usually written without modifying its source code. But if you want, inserting these two lines into func() may do:
ret = (int *)&str; // point behind saved return address
ret[-1] += 12; // or however many code bytes are to be skipped

Resources